Passed
Push — v2 ( 4a6afa...e35b5b )
by Brice
02:53
created

Task::hasParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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