Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AvailabilityAdd
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
 writeEntityUpdate
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 writeSpontaneousEntity
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
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\Connection\Select as DbConnection;
16use Psr\Http\Message\RequestInterface;
17use Psr\Http\Message\ResponseInterface;
18use BO\Zmsapi\AvailabilitySlotsUpdate;
19use BO\Zmsapi\Exception\BadRequest as BadRequestException;
20use BO\Zmsapi\Exception\Availability\AvailabilityUpdateFailed as UpdateFailedException;
21
22/**
23 * @SuppressWarnings(Coupling)
24 */
25class AvailabilityAdd extends BaseController
26{
27    /**
28     * @SuppressWarnings(Param)
29     * @return ResponseInterface
30     */
31    public function readResponse(
32        RequestInterface $request,
33        ResponseInterface $response,
34        array $args
35    ): ResponseInterface {
36        (new Helper\User($request))->checkRights();
37        $input = Validator::input()->isJson()->assertValid()->getValue();
38        if (! $input || count($input) === 0) {
39            throw new BadRequestException();
40        }
41        $collection = new Collection();
42        DbConnection::getWriteConnection();
43        foreach ($input as $item) {
44            $entity = new Entity($item);
45            $entity->testValid();
46            $updatedEntity = $this->writeEntityUpdate($entity);
47            AvailabilitySlotsUpdate::writeCalculatedSlots($updatedEntity, true);
48            $collection->addEntity($updatedEntity);
49        }
50
51        $message = Response\Message::create($request);
52        $message->data = $collection->getArrayCopy();
53
54        $response = Render::withLastModified($response, time(), '0');
55        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
56        return $response;
57    }
58
59    protected function writeEntityUpdate($entity): Entity
60    {
61        $repository = new AvailabilityRepository();
62        $updatedEntity = null;
63        if ($entity->id) {
64            $oldentity = $repository->readEntity($entity->id);
65            if ($oldentity && $oldentity->hasId()) {
66                $this->writeSpontaneousEntity($oldentity);
67                $updatedEntity = $repository->updateEntity($entity->id, $entity, 2);
68            }
69        } else {
70            $updatedEntity = $repository->writeEntity($entity, 2);
71        }
72        if (! $updatedEntity) {
73            throw new UpdateFailedException();
74        }
75        return $updatedEntity;
76    }
77
78    protected function writeSpontaneousEntity(Entity $entity): void
79    {
80        $doubleTypesEntity = (new AvailabilityRepository())->readEntityDoubleTypes($entity->id);
81        if ($doubleTypesEntity) {
82            $doubleTypesEntity->workstationCount['intern'] = 0;
83            $doubleTypesEntity->workstationCount['callcenter'] = 0;
84            $doubleTypesEntity->workstationCount['public'] = 0;
85            $doubleTypesEntity['description'] = '';
86            $doubleTypesEntity['type'] = 'openinghours';
87            (new AvailabilityRepository())->writeEntity($doubleTypesEntity);
88        }
89    }
90}