Passed
Push — master ( 1fece0...af95a9 )
by Hector Luis
03:59
created

Query::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
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\Signatures\PeriodSignature;
14
15
/**
16
 * Class Query
17
 * @package Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS
18
 */
19
class Query implements QuerySignature
20
{
21
    use Validator;
22
23
    /** @var array */
24
    private $list = [];
25
26
    /** @var string */
27
    private $format;
28
29
    /** @var Constraint\WeekDays */
30
    private $weekDaysConstraint;
31
32
    /** @var Constraint\Period */
33
    private $periodConstraint;
34
35
    /**
36
     * Query constructor.
37
     *
38
     * @param Constraint\WeekDays $weekDaysConstraint
39
     * @param Constraint\Period   $periodConstraint
40
     * @param string              $format
41
     */
42
    public function __construct(
43
        Constraint\WeekDays $weekDaysConstraint,
44
        Constraint\Period $periodConstraint,
45
        string $format = PeriodSignature::DEFAULT_FORMAT
46
    ) {
47
        $this->format = $format;
48
        $this->weekDaysConstraint = $weekDaysConstraint;
49
        $this->periodConstraint = $periodConstraint;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function interpret(string $rule, string $type): QuerySignature
56
    {
57
        $decoded = $this->decode($rule);
58
        $this->validate($decoded, $type);
59
        $result = [
60
            'single'        => (function () use ($decoded) {
61
                $period = $this->periodConstraint->extract([
62
                    'from' => $decoded['date'],
63
                    'to'   => $decoded['date'],
64
                ]);
65
66
                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...
67
            }),
68
            'period'        => (function () use ($decoded) {
69
                $period = $this->periodConstraint->extract($decoded);
70
71
                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...
72
            }),
73
            'recurrent_day' => (function () use ($decoded) {
74
                $dayOfWeek = jddayofweek($decoded['dayOfWeek'] - 1, 1);
75
                $period = $this->weekDaysConstraint->extract([
76
                    'weekDay' => $dayOfWeek,
77
                    'year'    => date("Y"),
78
                    'month'   => date("m"),
79
                ]);
80
81
                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...
82
            }),
83
        ];
84
        $result[$type]();
85
86
        return $this;
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function fetchList(): array
93
    {
94
        usort($this->list, function ($previous, $next) {
95
            $previousDate = strtotime($previous);
96
            $nextDate = strtotime($next);
97
98
            return ($previousDate - $nextDate);
99
        });
100
101
        return $this->list;
102
    }
103
104
    /**
105
     * @param $rule
106
     *
107
     * @return mixed
108
     * It's separated since it could change
109
     */
110
    private function decode($rule)
111
    {
112
        return json_decode($rule, true);
113
    }
114
115
    /**
116
     * @param DatePeriod $period
117
     * Given a period, extract proper dates based upon two constraints: greater than current date and not existing
118
     * in current list
119
     */
120
    private function extract(DatePeriod $period)
121
    {
122
        $today = date($this->format);
123
        /**
124
         * @var                   $key
125
         * @var DateTimeInterface $date
126
         */
127
        foreach ($period as $key => $date) {
128
            $value = $date->format($this->format);
129
            if ($value >= $today && !in_array($value, $this->list)) {
130
                $this->list[] = $value;
131
            }
132
        }
133
    }
134
}
135