Test Failed
Push — main ( d2d042...ea42f2 )
by Bingo
13:21
created

MetricsQueryImpl::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Jabe\Engine\Impl\Metrics;
4
5
use Jabe\Engine\ProcessEngineException;
6
use Jabe\Engine\Impl\Db\ListQueryParameterObject;
7
use Jabe\Engine\Impl\Interceptor\{
8
    CommandInterface,
9
    CommandContext,
10
    CommandExecutorInterface
11
};
12
use Jabe\Engine\Impl\Metrics\Util\MetricsUtil;
13
use Jabe\Engine\Management\{
14
    MetricIntervalValueInterface,
15
    MetricsQueryInterface
16
};
17
18
class MetricsQueryImpl extends ListQueryParameterObject implements \Serializable, CommandInterface, MetricsQueryInterface
19
{
20
    public const DEFAULT_LIMIT_SELECT_INTERVAL = 200;
21
    public const DEFAULT_SELECT_INTERVAL = 15 * 60;
22
23
    protected $name;
24
    protected $reporter;
25
    protected $startDate;
26
    protected $endDate;
27
    protected $startDateMilliseconds;
28
    protected $endDateMilliseconds;
29
    protected $interval;
30
    protected $aggregateByReporter;
31
    protected $commandExecutor;
32
33
    public function __construct(CommandExecutorInterface $commandExecutor)
34
    {
35
        $this->commandExecutor = $commandExecutor;
36
        $this->maxResults = self::DEFAULT_LIMIT_SELECT_INTERVAL;
37
        $this->interval = self::DEFAULT_SELECT_INTERVAL;
38
    }
39
40
    public function serialize()
41
    {
42
        return json_encode([
43
            'name' => $this->name,
44
            'reporter' => $this->reporter,
45
            'startDate' => $this->startDate,
46
            'endDate' => $this->endDate,
47
            'startDateMilliseconds' => $this->startDateMilliseconds,
48
            'endDateMilliseconds' => $this->endDateMilliseconds,
49
            'interval' => $this->interval,
50
            'aggregateByReporter' => $this->aggregateByReporter
51
        ]);
52
    }
53
54
    public function unserialize($data)
55
    {
56
        $json = json_decode($data);
57
        $this->name = $json->name;
58
        $this->reporter = $json->reporter;
59
        $this->startDate = $json->startDate;
60
        $this->endDate = $json->endDate;
61
        $this->startDateMilliseconds = $json->startDateMilliseconds;
62
        $this->endDateMilliseconds = $json->endDateMilliseconds;
63
        $this->interval = $json->interval;
64
        $this->aggregateByReporter = $json->aggregateByReporter;
65
    }
66
67
    public function name(string $name): MetricsQueryImpl
68
    {
69
        $this->name = MetricsUtil::resolveInternalName($name);
70
        return $this;
71
    }
72
73
    public function reporter(string $reporter): MetricsQueryInterface
74
    {
75
        $this->reporter = $reporter;
76
        return $this;
77
    }
78
79
    public function startDate(string $startDate): MetricsQueryImpl
80
    {
81
        $this->startDate = $startDate;
82
        $this->startDateMilliseconds = (new \DateTime($startDate))->getTimestamp() * 1000;
83
        return $this;
84
    }
85
86
    public function endDate(string $endDate): MetricsQueryImpl
87
    {
88
        $this->endDate = $endDate;
89
        $this->endDateMilliseconds = (new \DateTime($endDate))->getTimestamp() * 1000;
90
        return $this;
91
    }
92
93
    /**
94
     * Contains the command implementation which should be executed either
95
     * metric sum or select metric grouped by time interval.
96
     *
97
     * Note: this enables to quit with the enum distinction
98
     */
99
    protected $callback;
100
101
    public function interval(int $interval = null): array
102
    {
103
        if ($interval !== null) {
104
            $this->interval = $interval;
105
        }
106
        $this->callback = new MetricsQueryIntervalCmd($this);
107
        return $this->commandExecutor->execute($this);
108
    }
109
110
    public function sum(): int
111
    {
112
        $this->callback = new MetricsQuerySumCmd($this);
113
        return $this->commandExecutor->execute($this);
114
    }
115
116
    public function execute(CommandContext $commandContext)
117
    {
118
        if ($this->callback !== null) {
119
            return $this->callback->execute($commandContext);
120
        }
121
        throw new ProcessEngineException("Query can't be executed. Use either sum or interval to query the metrics.");
122
    }
123
124
    public function offset(int $offset): MetricsQueryInterface
125
    {
126
        $this->setFirstResult($offset);
127
        return $this;
128
    }
129
130
    public function limit(int $maxResults): MetricsQueryInterface
131
    {
132
        $this->setMaxResults($maxResults);
133
        return $this;
134
    }
135
136
    public function aggregateByReporter(): MetricsQueryInterface
137
    {
138
        $this->aggregateByReporter = true;
139
        return $this;
140
    }
141
142
    public function setMaxResults(int $maxResults): void
143
    {
144
        if ($maxResults > self::DEFAULT_LIMIT_SELECT_INTERVAL) {
145
            throw new ProcessEngineException("Metrics interval query row limit can't be set larger than " . self::DEFAULT_LIMIT_SELECT_INTERVAL . '.');
146
        }
147
        $this->maxResults = $maxResults;
148
    }
149
150
    public function getStartDate(): string
151
    {
152
        return $this->startDate;
153
    }
154
155
    public function getEndDate(): string
156
    {
157
        return $this->endDate;
158
    }
159
160
    public function getStartDateMilliseconds(): int
161
    {
162
        return $this->startDateMilliseconds;
163
    }
164
165
    public function getEndDateMilliseconds(): int
166
    {
167
        return $this->endDateMilliseconds;
168
    }
169
170
    public function getName(): string
171
    {
172
        return $this->name;
173
    }
174
175
    public function getReporter(): string
176
    {
177
        return $this->reporter;
178
    }
179
180
    public function getInterval(): int
181
    {
182
        if ($this->interval == null) {
183
            return self::DEFAULT_SELECT_INTERVAL;
184
        }
185
        return $this->interval;
186
    }
187
188
    public function getMaxResults(): int
189
    {
190
        if ($this->maxResults > self::DEFAULT_LIMIT_SELECT_INTERVAL) {
191
            return self::DEFAULT_LIMIT_SELECT_INTERVAL;
192
        }
193
        return parent::getMaxResults();
194
    }
195
}
196