<?php
// Orders.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;use Docuform\Api\Exception\ApiHttpNotFoundException;class Orders 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'=>[ApiScopes::server_read],'anyOf'=>[]];}public function handleRequest($request,& $response,$arguments):int{$result=404;if($request->getMethod()==='POST'){$order=new Order($this->getContainer());$order->create($request->getParsedBody());Order::buildJsonResponse($response,$order);Order::addResponseHeader($response,'Location',RestApi::getRequestUri().'/'.$order->id);$result=201;}elseif($request->getMethod()==='PATCH'){$serverApi=$this->getContainer()->get('RestApi');$order=$serverApi->getOrder();if($order!==false){$order->update($request->getParsedBody());$result=204;}}elseif($request->getMethod()==='DELETE'){$serverApi=$this->getContainer()->get('RestApi');$order=$serverApi->getOrder();if($order===null){throw new ApiHttpNotFoundException($request);}$order->delete();$result=204;}elseif($request->getMethod()==='GET'){if(empty($arguments)){$readResult=Order::readAll($this->getContainer());if(!empty($readResult['data'])){Order::buildJsonResponse($response,$readResult['data'],'orders',$readResult['meta']['pagination']);$result=200;}else{$result=204;}}else{$serverApi=$this->getContainer()->get('RestApi');$order=$serverApi->getOrder();if($order===null){throw new ApiHttpNotFoundException($request);}Order::buildJsonResponse($response,$order,'order');$result=200;}}return $result;}}