Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AvailabilityClosureToggle
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 readResponse
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
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\Closure;
13use BO\Zmsentities\Closure as ClosureEntity;
14use DateTime;
15use Psr\Http\Message\RequestInterface;
16use Psr\Http\Message\ResponseInterface;
17use BO\Zmsapi\Exception\BadRequest as BadRequestException;
18
19/**
20 * @SuppressWarnings(Coupling)
21 */
22class AvailabilityClosureToggle extends BaseController
23{
24    /**
25     * @SuppressWarnings(Param)
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        $input = Validator::input()->isJson()->assertValid()->getValue();
35        if (! $input || count($input) === 0) {
36            throw new BadRequestException();
37        }
38
39        $scopeId = $args['id'];
40        $date = $args['date'];
41        $closure = null;
42
43        try {
44            $closure = (new Closure())->readByScopeIdAndDate($scopeId, new DateTime($date));
45        } catch (\Exception $e) {
46            $closure = new ClosureEntity();
47        }
48
49        if (empty($closure->getId())) {
50            $closure = (new Closure())->createOne($scopeId, new DateTime($date));
51            $closure->existing = true;
52        } else {
53            (new Closure())->deleteEntity($closure->getId());
54            $closure->existing = false;
55        }
56
57        $message = Response\Message::create($request);
58        $message->data = $closure;
59
60        $response = Render::withLastModified($response, time(), '0');
61        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
62        return $response;
63    }
64}