Passed
Push — v2 ( e35b5b...7ad8a1 )
by Brice
04:59
created

Task::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.9256

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 3
dl 0
loc 19
ccs 8
cts 12
cp 0.6667
crap 5.9256
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace JobQueue\Domain\Task;
4
5
use JobQueue\Domain\Job\ExecutableJob;
6
7
final class Task implements \Serializable, \JsonSerializable
8
{
9
    /**
10
     *
11
     * @var string
12
     */
13
    private $identifier;
14
15
    /**
16
     *
17
     * @var Status
18
     */
19
    private $status;
20
21
    /**
22
     *
23
     * @var Profile
24
     */
25
    private $profile;
26
27
    /**
28
     *
29
     * @var string
30
     */
31
    private $jobName;
32
33
    /**
34
     *
35
     * @var int
36
     */
37
    private $createdAt;
38
39
    /**
40
     *
41
     * @var array
42
     */
43
    private $parameters;
44
45
    /**
46
     *
47
     * @param Profile       $profile
48
     * @param ExecutableJob $job
49
     * @param array         $parameters
50
     */
51 3
    public function __construct(Profile $profile, ExecutableJob $job, array $parameters = [])
52
    {
53 3
        $this->identifier = new Identifier;
54 3
        $this->status = new Status(Status::WAITING);
55 3
        $this->profile = $profile;
56 3
        $this->jobName = get_class($job);
57 3
        $this->createdAt = time();
58
59
        // Check that parameters are key/values
60 3
        foreach ($parameters as $name => $value) {
61
            if (!is_string($name)) {
62
                throw new \RuntimeException('All parameters must be named with a string key');
63
            }
64
65
            if (!is_scalar($value) or !is_null($value)) {
66
                throw new \RuntimeException(sprintf('Parameter %s must be a scalar or null'));
67
            }
68
        }
69 3
        $this->parameters = $parameters;
70 3
    }
71
72
    /**
73
     *
74
     * @return string
75
     */
76 7
    public function getIdentifier(): string
77
    {
78 7
        return $this->identifier;
79
    }
80
81
    /**
82
     *
83
     * @return Status
84
     */
85 7
    public function getStatus(): Status
86
    {
87 7
        return $this->status;
88
    }
89
90
    /**
91
     *
92
     * @param Status $status
93
     */
94 4
    public function updateStatus(Status $status)
95
    {
96 4
        $this->status = $status;
97 4
    }
98
99
    /**
100
     *
101
     * @return Profile
102
     */
103 7
    public function getProfile(): Profile
104
    {
105 7
        return $this->profile;
106
    }
107
108
    /**
109
     *
110
     * @return ExecutableJob
111
     */
112 2
    public function getJob(): ExecutableJob
113
    {
114 2
        return new $this->jobName;
115
    }
116
117
    /**
118
     *
119
     * @param bool $humanReadable
120
     * @return string
121
     */
122 5
    public function getJobName(bool $humanReadable = false): string
123
    {
124 5
        if ($humanReadable) {
125 5
            $name = explode('\\', $this->jobName);
126 5
            $name = array_pop($name);
127
128
            // Convert CamelCase to snake_case
129 5
            preg_match_all('/([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)/', $name, $matches);
130 5
            foreach ($matches[0] as &$match) {
131 5
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
132
            }
133
134 5
            if ('job' !== $lmatch = array_pop($matches[0])) {
135
                $matches[0] = $lmatch;
136
            }
137
138 5
            return implode('_', $matches[0]);
139
        }
140
141 3
        return $this->jobName;
142
    }
143
144
    /**
145
     *
146
     * @param string|null $format
147
     * @return mixed
148
     */
149 6
    public function getCreatedAt(string $format = null)
150
    {
151 6
        return $format
152 5
            ? date($format, $this->createdAt)
153 6
            : $this->createdAt;
154
    }
155
156
    /**
157
     *
158
     * @param string $name
159
     * @return bool
160
     */
161
    public function hasParameter(string $name): bool
162
    {
163
        return isset($this->parameters[$name]);
164
    }
165
166
    /**
167
     *
168
     * @return array
169
     */
170 3
    public function getParameters(): array
171
    {
172 3
        return $this->parameters;
173
    }
174
175
    /**
176
     *
177
     * @param string $name
178
     * @return mixed
179
     */
180
    public function getParameter(string $name)
181
    {
182
        if (!$this->hasParameter($name)) {
183
            throw new \RuntimeException(sprintf('Parameter "%s" does not exists', $name));
184
        }
185
186
        return $this->parameters[$name];
187
    }
188
189
    /**
190
     *
191
     * @return string
192
     */
193 6
    public function serialize(): string
194
    {
195 6
        return serialize([
196 6
            (string) $this->identifier,
197 6
            (string) $this->status,
198 6
            (string) $this->profile,
199 6
            $this->jobName,
200 6
            $this->createdAt,
201 6
            $this->parameters,
202
        ]);
203
    }
204
205
    /**
206
     *
207
     * @param string $serialized
208
     */
209 7
    public function unserialize($serialized)
210
    {
211 7
        $array = unserialize($serialized);
212
213 7
        $this->identifier = new Identifier($array[0]);
214 7
        $this->status = new Status($array[1]);
215 7
        $this->profile = new Profile($array[2]);
216 7
        $this->jobName = $array[3];
217 7
        $this->createdAt = $array[4];
218 7
        $this->parameters = $array[5];
219 7
    }
220
221
    /**
222
     *
223
     * @return array
224
     */
225 1
    public function jsonSerialize(): array
226
    {
227
        return [
228 1
            'identifier' => (string) $this->identifier,
229 1
            'status'     => (string) $this->status,
230 1
            'profile'    => (string) $this->profile,
231 1
            'job'        => $this->jobName,
232 1
            'date'       => $this->getCreatedAt('r'),
233 1
            'parameters' => $this->parameters,
234
        ];
235
    }
236
237
    /**
238
     *
239
     * @return string
240
     */
241 3
    public function __toString(): string
242
    {
243 3
        return $this->identifier;
244
    }
245
}
246