<?php
// Devices.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Server;use Docuform\Api\HttpRequestHandler;use Docuform\Api\Exception\ApiHttpNotFoundException;use Docuform\Api\UserType;use Docuform\Database\DbException;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()!=='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'){$device=new Device($this->getContainer());$device->create($request->getParsedBody());Device::buildJsonResponse($response,$device,'device');$result=201;}elseif($request->getMethod()==='PATCH'){$serverApi=$this->getContainer()->get('RestApi');$device=$serverApi->getDevice();if($device!==false){$device->update($request->getParsedBody());$result=204;}}elseif($request->getMethod()==='DELETE'){$serverApi=$this->getContainer()->get('RestApi');$device=$serverApi->getDevice();if($device===null){throw new ApiHttpNotFoundException($request);}$device->delete();$result=204;}elseif($request->getMethod()==='GET'){if(empty($arguments['deviceId'])){$readResult=Device::readAll($this->getContainer());if(!empty($readResult['data'])){$prependJson='"devices":';$appendJson=',"lastHostAddressChangeOn":"'.Device::readLastAddressChangeDate($this->getContainer()).'"';Device::buildJsonResponse($response,$readResult['data'],null,$readResult['meta']['pagination'],$prependJson,$appendJson);$result=200;}else{$result=204;}}else{$serverApi=$this->getContainer()->get('RestApi');$device=$serverApi->getDevice();if($device===null){throw new ApiHttpNotFoundException($request);}Device::buildJsonResponse($response,$device,'device');$result=200;}}return $result;}}