Query::interpret()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 2
dl 0
loc 32
rs 9.6
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Domain Policy Class
5
 * @package Ticaje_BookingApi
6
 * @author  Hector Luis Barrientos <[email protected]>
7
 */
8
9
namespace Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS;
10
11
use DatePeriod;
12
use DateTimeInterface;
13
use Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\Constraint\Period;
14
use Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\Constraint\WeekDays;
15
use Ticaje\BookingApi\Domain\Signatures\PeriodSignature;
16
17
/**
18
 * Class Query
19
 * @package Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS
20
 */
21
class Query implements QuerySignature
22
{
23
    use Validator;
24
25
    /** @var array */
26
    private $list = [];
27
28
    /** @var string */
29
    private $format;
30
31
    /** @var WeekDays  */
32
    private $weekDaysConstraint;
33
34
    /** @var Period  */
35
    private $periodConstraint;
36
37
    /**
38
     * Query constructor.
39
     *
40
     * @param WeekDays $weekDaysConstraint
41
     * @param Period   $periodConstraint
42
     * @param string   $format
43
     */
44
    public function __construct(
45
        WeekDays $weekDaysConstraint,
46
        Period $periodConstraint,
47
        string $format = PeriodSignature::DEFAULT_FORMAT
48
    ) {
49
        $this->format = $format;
50
        $this->weekDaysConstraint = $weekDaysConstraint;
51
        $this->periodConstraint = $periodConstraint;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function interpret(string $rule, string $type): QuerySignature
58
    {
59
        $decoded = $this->decode($rule);
60
        $this->validate($decoded, $type);
61
        $result = [
62
            'single'        => (function () use ($decoded) {
63
                $period = $this->periodConstraint->extract([
64
                    'from' => $decoded['date'],
65
                    'to'   => $decoded['date'],
66
                ]);
67
68
                return $this->extract($period);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->extract($period) targeting Ticaje\BookingApi\Domain...g\CQRS\Query::extract() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
69
            }),
70
            'period'        => (function () use ($decoded) {
71
                $period = $this->periodConstraint->extract($decoded);
72
73
                return $this->extract($period);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->extract($period) targeting Ticaje\BookingApi\Domain...g\CQRS\Query::extract() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
            }),
75
            'recurrent_day' => (function () use ($decoded) {
76
                $dayOfWeek = jddayofweek($decoded['dayOfWeek'] - 1, 1);
77
                $period = $this->weekDaysConstraint->extract([
78
                    'weekDay' => $dayOfWeek,
79
                    'year'    => date("Y"),
80
                    'month'   => date("m"),
81
                ]);
82
83
                return $this->extract($period);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->extract($period) targeting Ticaje\BookingApi\Domain...g\CQRS\Query::extract() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
            }),
85
        ];
86
        $result[$type]();
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param $rule
93
     *
94
     * @return mixed
95
     * It's separated since it could change
96
     */
97
    private function decode($rule)
98
    {
99
        return json_decode($rule, true);
100
    }
101
102
    /**
103
     * @param DatePeriod $period
104
     * Given a period, extract proper dates based upon two constraints: greater than current date and not existing
105
     * in current list
106
     */
107
    private function extract(DatePeriod $period)
108
    {
109
        $today = date($this->format);
110
        /**
111
         * @var                   $key
112
         * @var DateTimeInterface $date
113
         */
114
        foreach ($period as $key => $date) {
115
            $value = $date->format($this->format);
116
            if ($value >= $today && !in_array($value, $this->list)) {
117
                $this->list[] = $value;
118
            }
119
        }
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function fetchList(): array
126
    {
127
        usort($this->list, function ($previous, $next) {
128
            $previousDate = strtotime($previous);
129
            $nextDate = strtotime($next);
130
131
            return ($previousDate - $nextDate);
132
        });
133
134
        return $this->list;
135
    }
136
}
137