<?php
// Customers.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Server;use Docuform\Api\HttpRequestHandler;use Docuform\Api\RestApi;use Docuform\Api\UserType;use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;use Docuform\Api\ApiScopes;class Customers extends HttpRequestHandler{public function getAllowedUserTypes($request):array{return[UserType::USERTYPE_CUSTOMER,UserType::USERTYPE_DEALER,UserType::USERTYPE_ADMIN];}public function getRequiredScopes($request):array{if($request->getMethod()!=='GET')return['allOf'=>[ApiScopes::server_read,ApiScopes::server_write],'anyOf'=>[]];else return['allOf'=>[],'anyOf'=>[ApiScopes::server_read,ApiScopes::mcs]];}public function handleRequest($request,& $response,$arguments):int{$result=404;if($request->getMethod()==='POST'){$customer=new Customer($this->getContainer());$customer->create($request->getParsedBody());Customer::buildJsonResponse($response,$customer);Customer::addResponseHeader($response,'Location',RestApi::getRequestUri().'/'.$customer->id);$result=201;}elseif($request->getMethod()==='PATCH'){$serverApi=$this->getContainer()->get('RestApi');$customer=$serverApi->getCustomer();if($customer!==false){$customer->update($request->getParsedBody());$result=204;}}elseif($request->getMethod()==='DELETE'){$serverApi=$this->getContainer()->get('RestApi');$customer=$serverApi->getCustomer();if($customer!==false){$customer->delete();$result=204;}}elseif($request->getMethod()==='GET'){if(empty($arguments)){$readResult=Customer::readAll($this->getContainer());if(!empty($readResult['data'])){Customer::buildJsonResponse($response,$readResult['data'],'customers',$readResult['meta']['pagination']);$result=200;}else{$result=204;}}else{$serverApi=$this->getContainer()->get('RestApi');$customer=$serverApi->getCustomer();if($customer!==false&&$customer->id>0){Customer::buildJsonResponse($response,$customer,'customer');$result=200;}}}return $result;}}