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