Lines
82.35%
28 / 34
Functions and Methods
50.00%
1 / 2
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AvailabilitySlotsUpdate | 82.35% | 28 / 34 | 50.00% | 1 / 2 | 10.55 | 0.00% | 0 / 1 | |||
| readResponse | 100.00% | 19 / 19 | 100.00% | 1 / 1 | 5 | |||||
| writeCalculatedSlots | 60.00% | 9 / 15 | 0.00% | 0 / 1 | 6.60 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | /** | |
| 4 | * @package ZMS API | |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG | |
| 6 | **/ | |
| 7 | ||
| 8 | namespace BO\Zmsbackend\Availability\Api; | |
| 9 | ||
| 10 | use BO\Slim\Render; | |
| 11 | use BO\Mellon\Validator; | |
| 12 | use BO\Zmsentities\Availability as Entity; | |
| 13 | use BO\Zmsentities\Collection\AvailabilityList as Collection; | |
| 14 | use BO\Zmsbackend\Availability\Service\Availability as AvailabilityRepository; | |
| 15 | use BO\Zmsbackend\Slot\Service\Slot as SlotRepository; | |
| 16 | use BO\Zmsbackend\Config\Service\Config as ConfigRepository; | |
| 17 | use BO\Zmsbackend\Helper\CalculateSlots as CalculateSlotsHelper; | |
| 18 | use BO\Zmsbackend\Connection\Select as DbConnection; | |
| 19 | use Psr\Http\Message\RequestInterface; | |
| 20 | use Psr\Http\Message\ResponseInterface; | |
| 21 | use BO\Zmsbackend\Exception\BadRequest as BadRequestException; | |
| 22 | use BO\Zmsbackend\Availability\Exception\AvailabilityNotFound as NotfoundException; | |
| 23 | ||
| 24 | /** | |
| 25 | * @SuppressWarnings(Coupling) | |
| 26 | */ | |
| 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 | } |