<?php
// Devices.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\ServerRequestInterface;use Psr\Http\Message\ResponseInterface;use Docuform\Api\ApiScopes;class Devices 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'=>[],'anyOf'=>[ApiScopes::client_write,ApiScopes::bulk]];else return['allOf'=>[],'anyOf'=>[ApiScopes::client_read,ApiScopes::bulk]];}public function handleRequest($request,& $response,$arguments):int{$result=404;if($request->getMethod()==='POST'){$device=new Device($this->getContainer());$result=$device->create($request->getParsedBody());if($result===201){$response=$response->withHeader('Location',RestApi::getRequestUri().'/'.$device->id);}$payload=json_encode($device);$response->getBody()->write($payload);}elseif($request->getMethod()==='DELETE'){$clientApi=$this->getContainer()->get('RestApi');$device=$clientApi->getDevice();if($device===null){throw new ApiHttpNotFoundException($request);}$result=$device->delete();}elseif($request->getMethod()==='GET'){if(empty($arguments['deviceId'])){$devices=Device::readAll($this->getContainer());if(!empty($devices)){Device::buildJsonResponse($response,$devices,'devices');$result=200;}else{$result=204;}}else{$clientApi=$this->getContainer()->get('RestApi');$device=$clientApi->getDevice();if($device===null){throw new ApiHttpNotFoundException($request);}Device::buildJsonResponse($response,$device,'device');$result=200;}}return $result;}}