<?php
// DeviceItems.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;class DeviceItems 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'){$deviceItem=new DeviceItem($this->getContainer());$deviceItem->create($request->getParsedBody());if($deviceItem->item['id']){DeviceItem::buildJsonResponse($response,$deviceItem);DeviceItem::addResponseHeader($response,'Location',RestApi::getRequestUri().'/'.$deviceItem->item['id']);$result=201;}}elseif($request->getMethod()==='DELETE'){$serverApi=$this->getContainer()->get('RestApi');$deviceItem=$serverApi->getDeviceItem();if($deviceItem!==false){$deviceItem->delete();$result=204;}}elseif($request->getMethod()==='GET'){if(empty($arguments['itemId'])){$deviceItems=DeviceItem::readAll($this->getContainer());if(is_array($deviceItems)&&!empty($deviceItems)){$prependJson='"deviceId":'.$arguments['deviceId'].','.'"items":';DeviceEvent::buildJsonResponse($response,$deviceItems,'deviceItems',null,$prependJson);$result=200;}else{$result=204;}}else{$serverApi=$this->getContainer()->get('RestApi');$deviceItem=$serverApi->getDeviceItem();if($deviceItem!==false&&!empty($deviceItem->item['id'])){DeviceItem::buildJsonResponse($response,$deviceItem,'deviceItem');$result=200;}}}return $result;}}