Lines
83.78%
31 / 37
Methods
66.66%
2 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| readResponse | 100.00% | 19 / 19 | 100.00% | 1 / 1 | 5 | ||
| writeCalculatedSlots | 60.00% | 9 / 15 | 0.00% | 0 / 1 | 6.60 | ||
| [BO\Zmsbackend\Api\BaseController] __invoke | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| 27 | class AvailabilitySlotsUpdate extends \BO\Zmsbackend\Api\BaseController | |
| 28 | { | |
| 29 | /** | |
| 30 | * @SuppressWarnings(Param) | |
| 31 | * @return ResponseInterface | |
| 32 | */ | |
| 33 | #[\Override] | |
| 34 | public function readResponse( | |
| 35 | RequestInterface $request, | |
| 36 | ResponseInterface $response, | |
| 37 | array $args | |
| 38 | ): ResponseInterface { | |
| 39 | (new \BO\Zmsbackend\Helper\User($request))->checkPermissions('availability'); | |
| 40 | $input = Validator::input()->isJson()->assertValid()->getValue(); | |
| 41 | if (! $input || count($input) === 0) { | |
| 42 | throw new BadRequestException(); | |
| 43 | } | |
| 44 | $collection = new Collection(); | |
| 45 | DbConnection::getWriteConnection(); | |
| 46 | foreach ($input as $item) { | |
| 47 | $entity = new Entity($item); | |
| 48 | $entity->testValid(); | |
| 49 | $availability = (new AvailabilityRepository())->readEntity($entity->getId(), 2); | |
| 50 | if (! $availability->hasId()) { | |
| 51 | throw new NotfoundException(); | |
| 52 | } | |
| 53 | static::writeCalculatedSlots($availability); | |
| 54 | $collection->addEntity($availability); | |
| 55 | } | |
| 56 | ||
| 57 | $message = \BO\Zmsbackend\Api\Response\Message::create($request); | |
| 58 | $message->data = $collection->getArrayCopy(); | |
| 59 | ||
| 60 | $response = Render::withLastModified($response, time(), '0'); | |
| 61 | $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode()); | |
| 62 | return $response; | |
| 63 | } | |
| 64 | ||
| 65 | public static function writeCalculatedSlots(Entity $availability, bool $checkConfigOnSave = false): void | |
| 66 | { | |
| 67 | $config = (new ConfigRepository())->readEntity(); | |
| 68 | if ( | |
| 69 | $checkConfigOnSave && in_array( | |
| 70 | getenv('ZMS_ENV'), | |
| 71 | explode(',', $config->getPreference('availability', 'calculateSlotsOnSave')) | |
| 72 | ) | |
| 73 | ) { | |
| 74 | (new SlotRepository())->writeByAvailability($availability, \App::$now); | |
| 75 | (new CalculateSlotsHelper(\App::DEBUG)) | |
| 76 | ->writePostProcessingByScope($availability->scope, \App::$now); | |
| 77 | } | |
| 78 | ||
| 79 | if ( | |
| 80 | ! $checkConfigOnSave && in_array( | |
| 81 | getenv('ZMS_ENV'), | |
| 82 | explode(',', $config->getPreference('availability', 'calculateSlotsOnDemand')) | |
| 83 | ) | |
| 84 | ) { | |
| 85 | (new SlotRepository())->writeByAvailability($availability, \App::$now); | |
| 86 | (new CalculateSlotsHelper(\App::DEBUG)) | |
| 87 | ->writePostProcessingByScope($availability->scope, \App::$now); | |
| 88 | } | |
| 89 | } | |
| 90 | } |
Inherited from BO\Zmsbackend\Api\BaseController
| 20 | public function __invoke(RequestInterface $request, ResponseInterface $response, array $args) | |
| 21 | { | |
| 22 | $request = $this->initRequest($request); | |
| 23 | $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0'); | |
| 24 | return $this->readResponse($request, $noCacheResponse, $args); | |
| 25 | } |