<?php
// DeviceBulks.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\ApiHttpBadRequestException;use Docuform\Api\Exception\ApiHttpNotFoundException;use Docuform\Api\UserType;use Psr\Http\Message\ServerRequestInterface;use Psr\Http\Message\ResponseInterface;use Docuform\Api\ApiScopes;class DeviceBulks extends HttpRequestHandler{public function getAllowedUserTypes($request):array{return[UserType::USERTYPE_CUSTOMER,UserType::USERTYPE_DEALER,UserType::USERTYPE_ADMIN];}public function getRequiredScopes($request):array{return['allOf'=>[ApiScopes::bulk],'anyOf'=>[]];}public function handleRequest($request,& $response,$arguments):int{$result=404;if($request->getMethod()==='POST'){$bulk=new Bulk($this->getContainer());$result=$bulk->create($request->getParsedBody());if($result===201){$response=$response->withHeader('Location',RestApi::getRequestUri().'/'.$bulk->id);}$payload=json_encode($bulk);$response->getBody()->write($payload);}elseif($request->getMethod()==='DELETE'){$clientApi=$this->getContainer()->get('RestApi');$bulk=$clientApi->getBulk();if($bulk===null){throw new ApiHttpNotFoundException($request);}$result=$bulk->delete();}elseif($request->getMethod()==='GET'){if(empty($arguments['bulkId'])){$clientApi=$this->getContainer()->get('RestApi');$customer=$clientApi->getCustomer();$device=$clientApi->getDevice();if($customer===null||$device===null){throw new ApiHttpBadRequestException($request);}$bulks=Bulk::readAll($this->getContainer());if(!empty($bulks)){Bulk::buildJsonResponse($response,$bulks,'deviceBulks');$result=200;}else{$result=204;}}else{$clientApi=$this->getContainer()->get('RestApi');$bulk=$clientApi->getBulk();if($bulk===null){throw new ApiHttpNotFoundException($request);}Bulk::buildJsonResponse($response,$bulk,'deviceBulk');$result=200;}}return $result;}}