aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Consultations/SlotCreationCount.php
blob: c378771c6aa126996f1f02ce4bc5c001015031bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace JsonApi\Routes\Consultations;

use ConsultationBlock;
use JsonApi\Errors\BadRequestException;
use JsonApi\NonJsonApiController;
use Neomerx\JsonApi\Exceptions\JsonApiException;
use Neomerx\JsonApi\Schema\ErrorCollection;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

final class SlotCreationCount extends NonJsonApiController
{
    public function __invoke(Request $request, Response $response, array $args)
    {
        $parameters = $request->getQueryParams();

        $this->validateParameters($parameters);

        // Determine duration of a slot and pause times
        $slot_count = ConsultationBlock::countSlots(
            strtotime($parameters['start']),
            strtotime($parameters['end']),
            $parameters['dow'],
            $parameters['interval'],
            $parameters['duration'],
            $parameters['pause_time'] ?? null,
            $parameters['pause_duration'] ?? null
        );

        $response->getBody()->write((string) $slot_count);
        return $response->withAddedHeader('Content-Type', 'application/json');
    }

    private function validateParameters(array $parameters): void
    {
        $collection = new ErrorCollection();

        foreach (['start', 'end', 'dow', 'interval', 'duration'] as $key) {
            if (!isset($parameters[$key])) {
                $collection->addQueryParameterError($key, 'Parameter is missing');
            }
        }

        if (isset($parameters['start'], $parameters['end'])) {
            $start = strtotime($parameters['start']);
            $end = strtotime($parameters['end']);

            if (!$start) {
                $collection->addQueryParameterError('start', 'Parameter has invalid datetime format');
            }

            if (!$end) {
                $collection->addQueryParameterError('end', 'Parameter has invalid datetime format');
            }

            if ($start && $end && $start > $end) {
                $collection->addQueryParameterError('start', 'Datetime value of start must be before end');
            }
        }

        if (
            isset($parameters['dow'])
            && (
                !ctype_digit($parameters['dow'])
                || $parameters['dow'] < 0
                || $parameters['dow'] > 6
            )
        ) {
            $collection->addQueryParameterError('dow', 'Parameter must be a number between 0 and 6');
        }

        if (
            isset($parameters['interval'])
            && (
                !ctype_digit($parameters['interval'])
                || $parameters['interval'] < 0
                || $parameters['interval'] > 4
            )
        ) {
            $collection->addQueryParameterError('interval', 'Parameter must be a number between 0 and 4');
        }

        if (
            isset($parameters['duration'])
            && (
                !ctype_digit($parameters['duration'])
                || $parameters['duration'] <= 0
            )
        ) {
            $collection->addQueryParameterError('duration', 'Parameter must be a positive number');
        }

        if (
            isset($parameters['pause_time'], $parameters['duration'])
            && $parameters['pause_time'] < $parameters['duration']
        ) {
            $collection->addQueryParameterError('pause_time', 'The defined time to a pause is shorter than the duration of a slot.');
        }

        if (count($collection) > 0) {
            throw new JsonApiException($collection);
        }
    }
}