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    public function readResponse(
34        RequestInterface $request,
35        ResponseInterface $response,
36        array $args
37    ): ResponseInterface {
38        (new Helper\User($request))->checkRights();
39        $input = Validator::input()->isJson()->assertValid()->getValue();
40        if (! $input || count($input) === 0) {
41            throw new BadRequestException();
42        }
43        $collection = new Collection();
44        DbConnection::getWriteConnection();
45        foreach ($input as $item) {
46            $entity = new Entity($item);
47            $entity->testValid();
48            $availability = (new AvailabilityRepository())->readEntity($entity->getId(), 2);
49            if (! $availability->hasId()) {
50                throw new NotfoundException();
51            }
52            static::writeCalculatedSlots($availability);
53            $collection->addEntity($availability);
54        }
55
56        $message = Response\Message::create($request);
57        $message->data = $collection->getArrayCopy();
58
59        $response = Render::withLastModified($response, time(), '0');
60        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
61        return $response;
62    }
63
64    public static function writeCalculatedSlots(Entity $availability, bool $checkConfigOnSave = false)
65    {
66        $config = (new ConfigRepository())->readEntity();
67        if (
68            $checkConfigOnSave && in_array(
69                getenv('ZMS_ENV'),
70                explode(',', $config->getPreference('availability', 'calculateSlotsOnSave'))
71            )
72        ) {
73            (new SlotRepository())->writeByAvailability($availability, \App::$now);
74            (new CalculateSlotsHelper(\App::DEBUG))
75                ->writePostProcessingByScope($availability->scope, \App::$now);
76        }
77
78        if (
79            ! $checkConfigOnSave && in_array(
80                getenv('ZMS_ENV'),
81                explode(',', $config->getPreference('availability', 'calculateSlotsOnDemand'))
82            )
83        ) {
84            (new SlotRepository())->writeByAvailability($availability, \App::$now);
85            (new CalculateSlotsHelper(\App::DEBUG))
86                ->writePostProcessingByScope($availability->scope, \App::$now);
87        }
88    }
89}