Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.00% covered (warning)
76.00%
19 / 25
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AvailabilityUpdate
76.00% covered (warning)
76.00%
19 / 25
50.00% covered (danger)
50.00%
1 / 2
4.22
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
 writeSpontaneousEntity
25.00% covered (danger)
25.00%
2 / 8
0.00% covered (danger)
0.00%
0 / 1
3.69
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\Zmsdb\Availability as AvailabilityRepository;
13use BO\Zmsdb\Connection\Select as DbConnection;
14use BO\Zmsentities\Availability as Entity;
15use Psr\Http\Message\RequestInterface;
16use Psr\Http\Message\ResponseInterface;
17use BO\Zmsapi\AvailabilitySlotsUpdate;
18use BO\Zmsapi\Exception\Availability\AvailabilityNotFound as NotfoundException;
19
20/**
21 * @SuppressWarnings(Coupling)
22 */
23class AvailabilityUpdate extends BaseController
24{
25    /**
26     * @return ResponseInterface
27     */
28    public function readResponse(
29        RequestInterface $request,
30        ResponseInterface $response,
31        array $args
32    ): ResponseInterface {
33        (new Helper\User($request))->checkRights();
34        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
35        $input = Validator::input()->isJson()->assertValid()->getValue();
36        $entity = new Entity($input);
37        $entity->testValid();
38
39        $availability = (new AvailabilityRepository())->readEntity($args['id'], $resolveReferences);
40        if (! $availability->hasId()) {
41            throw new NotfoundException();
42        }
43
44        DbConnection::getWriteConnection();
45        $this->writeSpontaneousEntity($availability);
46        $updatedEntity = (new AvailabilityRepository())->updateEntity($args['id'], $entity, $resolveReferences);
47        AvailabilitySlotsUpdate::writeCalculatedSlots($updatedEntity, true);
48
49        $message = Response\Message::create($request);
50        $message->data = $updatedEntity;
51
52        $response = Render::withLastModified($response, time(), '0');
53        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
54        return $response;
55    }
56
57    protected function writeSpontaneousEntity(Entity $entity): void
58    {
59        $doubleTypesEntity = (new AvailabilityRepository())->readEntityDoubleTypes($entity->id);
60        if ($doubleTypesEntity) {
61            $doubleTypesEntity->workstationCount['intern'] = 0;
62            $doubleTypesEntity->workstationCount['callcenter'] = 0;
63            $doubleTypesEntity->workstationCount['public'] = 0;
64            $doubleTypesEntity['description'] = '';
65            $doubleTypesEntity['type'] = 'openinghours';
66            (new AvailabilityRepository())->writeEntity($doubleTypesEntity);
67        }
68    }
69}