Passed
Branch master (aecce8)
by Brice
05:03
created

Task::getJobName()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 7
nop 1
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
crap 5.0187
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 ParameterBag
42
     */
43
    private $parameters;
44
45
    /**
46
47
     * @var TagBag
48
     */
49
    private $tags;
50
51
    /**
52
     *
53
     * @param Profile       $profile
54
     * @param ExecutableJob $job
55
     * @param array         $parameters
56
     * @param array         $tags
57
     */
58 13
    public function __construct(Profile $profile, ExecutableJob $job, array $parameters = [], array $tags = [])
59
    {
60 13
        $this->identifier = new Identifier;
61 13
        $this->status = new Status(Status::WAITING);
62 13
        $this->profile = $profile;
63 13
        $this->jobName = get_class($job);
64 13
        $this->createdAt = time();
65 13
        $this->parameters = new ParameterBag($parameters);
66 11
        $this->tags = new TagBag($tags);
67 10
    }
68
69
    /**
70
     *
71
     * @return string
72
     */
73 17
    public function getIdentifier(): string
74
    {
75 17
        return $this->identifier;
76
    }
77
78
    /**
79
     *
80
     * @return Status
81
     */
82 17
    public function getStatus(): Status
83
    {
84 17
        return $this->status;
85
    }
86
87
    /**
88
     *
89
     * @param Status $status
90
     */
91 9
    public function updateStatus(Status $status)
92
    {
93 9
        $this->status = $status;
94 9
    }
95
96
    /**
97
     *
98
     * @return Profile
99
     */
100 16
    public function getProfile(): Profile
101
    {
102 16
        return $this->profile;
103
    }
104
105
    /**
106
     *
107
     * @return ExecutableJob
108
     */
109 4
    public function getJob(): ExecutableJob
110
    {
111 4
        return new $this->jobName;
112
    }
113
114
    /**
115
     *
116
     * @param bool $humanReadable
117
     * @return string
118
     */
119 7
    public function getJobName(bool $humanReadable = false): string
120
    {
121 7
        if ($humanReadable) {
122 7
            $name = explode('\\', $this->jobName);
123 7
            $name = array_pop($name);
124
125
            // Convert CamelCase to snake_case
126 7
            preg_match_all('/([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)/', $name, $matches);
127 7
            foreach ($matches[0] as &$match) {
128 7
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
129
            }
130
131 7
            if ('job' !== $lmatch = array_pop($matches[0])) {
132
                $matches[0] = $lmatch;
133
            }
134
135 7
            return implode('_', $matches[0]);
136
        }
137
138 3
        return $this->jobName;
139
    }
140
141
    /**
142
     *
143
     * @param string|null $format
144
     * @return mixed
145
     */
146 13
    public function getCreatedAt(string $format = null)
147
    {
148 13
        return $format
149 9
            ? date($format, $this->createdAt)
150 13
            : $this->createdAt;
151
    }
152
153
    /**
154
     *
155
     * @param string $name
156
     * @return bool
157
     */
158 1
    public function hasParameter(string $name): bool
159
    {
160 1
        return $this->parameters->has($name);
161
    }
162
163
    /**
164
     *
165
     * @return array
166
     */
167 3
    public function getParameters(): array
168
    {
169 3
        return $this->parameters->__toArray();
170
    }
171
172
    /**
173
     *
174
     * @param string $name
175
     * @return mixed
176
     */
177 1
    public function getParameter(string $name)
178
    {
179 1
        return $this->parameters->get($name);
180
    }
181
182
    /**
183
     *
184
     * @param string $name
185
     * @return bool
186
     */
187 3
    public function hasTag(string $name): bool
188
    {
189 3
        return $this->tags->has($name);
190
    }
191
192
    /**
193
     *
194
     * @return array
195
     */
196 5
    public function getTags(): array
197
    {
198 5
        return $this->tags->__toArray();
199
    }
200
201
    /**
202
     *
203
     * @return string
204
     */
205 13
    public function serialize(): string
206
    {
207 13
        return serialize([
208 13
            (string) $this->identifier,
209 13
            (string) $this->status,
210 13
            (string) $this->profile,
211 13
            $this->jobName,
212 13
            $this->createdAt,
213 13
            $this->parameters->__toArray(),
214 13
            $this->tags->__toArray(),
215
        ]);
216
    }
217
218
    /**
219
     *
220
     * @param string $serialized
221
     */
222 18
    public function unserialize($serialized)
223
    {
224 18
        $array = unserialize($serialized);
225
226 18
        $this->identifier = new Identifier($array[0]);
227 18
        $this->status = new Status($array[1]);
228 18
        $this->profile = new Profile($array[2]);
229 18
        $this->jobName = $array[3];
230 18
        $this->createdAt = $array[4];
231 18
        $this->parameters = new ParameterBag($array[5]);
232 18
        $this->tags = new TagBag($array[6]);
233 18
    }
234
235
    /**
236
     *
237
     * @return array
238
     */
239 3
    public function jsonSerialize(): array
240
    {
241
        return [
242 3
            'identifier' => (string) $this->identifier,
243 3
            'status'     => (string) $this->status,
244 3
            'profile'    => (string) $this->profile,
245 3
            'job'        => $this->jobName,
246 3
            'date'       => $this->getCreatedAt('r'),
247 3
            'parameters' => $this->parameters->__toArray(),
248 3
            'tags'       => $this->tags->__toArray(),
249
        ];
250
    }
251
252
    /**
253
     *
254
     * @return string
255
     */
256 7
    public function __toString(): string
257
    {
258 7
        return $this->identifier;
259
    }
260
}
261