<?php
// DeviceEvents.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Server;use Docuform\Api\ApiScopes;use Docuform\Api\Exception\ApiHttpBadRequestException;use Docuform\Api\HttpRequestHandler;use Docuform\Api\UserType;use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;class DeviceEvents 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()==='GET'){$serverApi=$this->getContainer()->get('RestApi');$device=$serverApi->getDevice();if(empty($device)){throw new ApiHttpBadRequestException($request);}$deviceId=$device!==false?$device->id:null;if(!empty($arguments['eventId'])){$deviceEvent=$serverApi->getDeviceEvent();if($deviceEvent!==false&&!empty($deviceEvent->eventId)){DeviceEvent::buildJsonResponse($response,$deviceEvent,'deviceEvent');$result=200;}}else{$readResult=DeviceEvent::readAll($this->getContainer(),$deviceId);if(!empty($readResult['data'])){$prependJson='"deviceId":'.$arguments['deviceId'].','.'"events":';DeviceEvent::buildJsonResponse($response,$readResult['data'],'deviceEvents',$readResult['meta']['pagination'],$prependJson);$result=200;}else{$result=204;}}}return $result;}}