<?php
// Customers.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Client;use Docuform\Api\HttpRequestHandler;use Docuform\Api\RestApi;use Docuform\Api\Exception\ApiHttpNotFoundException;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()==='POST'||$request->getMethod()==='PATCH'||$request->getMethod()==='DELETE')return['allOf'=>[ApiScopes::client_write],'anyOf'=>[]];else return['allOf'=>[],'anyOf'=>[ApiScopes::client_read,ApiScopes::bulk]];}public function handleRequest($request,& $response,$arguments):int{$result=404;if($request->getMethod()==='POST'){$customer=new Customer($this->getContainer());$result=$customer->create($request->getParsedBody());if($result===201){Customer::addResponseHeader($response,'Location',RestApi::getRequestUri().'/'.$customer->id);}Customer::buildJsonResponse($response,$customer);}elseif($request->getMethod()==='DELETE'){$clientApi=$this->getContainer()->get('RestApi');$customer=$clientApi->getCustomer();if($customer===null){throw new ApiHttpNotFoundException($request);}$result=$customer->delete();}elseif($request->getMethod()==='GET'){if(empty($arguments)){$customers=Customer::readAll($this->getContainer());if(!empty($customers)){Customer::buildJsonResponse($response,$customers,'customers');$result=200;}else{$result=204;}}else{$clientApi=$this->getContainer()->get('RestApi');$customer=$clientApi->getCustomer();if($customer===null){throw new ApiHttpNotFoundException($request);}Customer::buildJsonResponse($response,$customer,'customer');$result=200;}}return $result;}}