Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ChecksArrivalsAndDepartures
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
17
100.00% covered (success)
100.00%
1 / 1
 isArrivalDateValid
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 isDepartureDateValid
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 toCarbon
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 isBeforeBuildup
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isAfterTeardown
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers;
6
7use Carbon\Carbon;
8use DateTime;
9
10trait ChecksArrivalsAndDepartures
11{
12    protected function isArrivalDateValid(?string $arrival, ?string $departure): bool
13    {
14        $arrival_carbon = $this->toCarbon($arrival);
15        $departure_carbon = $this->toCarbon($departure);
16
17        if (is_null($arrival_carbon)) {
18            return false; // since required value
19        }
20
21        if (!is_null($departure_carbon) && $arrival_carbon->greaterThan($departure_carbon)) {
22            return false;
23        }
24
25        return !$this->isBeforeBuildup($arrival_carbon) && !$this->isAfterTeardown($arrival_carbon);
26    }
27
28    protected function isDepartureDateValid(?string $arrival, ?string $departure): bool
29    {
30        $arrival_carbon = $this->toCarbon($arrival);
31        $departure_carbon = $this->toCarbon($departure);
32
33        if (is_null($departure_carbon)) {
34            return true; // since optional value
35        }
36
37        if (is_null($arrival_carbon)) {
38            return false; // Will be false any ways
39        }
40
41        return $departure_carbon->greaterThanOrEqualTo($arrival_carbon) &&
42            !$this->isBeforeBuildup($departure_carbon) && !$this->isAfterTeardown($departure_carbon);
43    }
44
45    private function toCarbon(?string $date_string): ?Carbon
46    {
47        $dateTime = DateTime::createFromFormat('Y-m-d', $date_string ?: '');
48        return $dateTime ? new Carbon($dateTime) : null;
49    }
50
51    private function isBeforeBuildup(Carbon $date): bool
52    {
53        $buildup = config('buildup_start');
54        return !empty($buildup) && $date->lessThan($buildup->startOfDay());
55    }
56
57    private function isAfterTeardown(Carbon $date): bool
58    {
59        $teardown = config('teardown_end');
60        return !empty($teardown) && $date->greaterThanOrEqualTo($teardown->endOfDay());
61    }
62}