Job::getPayloadClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Resque;
6
7
use Psr\Container\ContainerInterface;
8
use Resque\Dispatchers\Noop;
9
use Resque\Exceptions\PayloadCorrupt;
10
use Resque\Interfaces\Dispatcher;
11
use Resque\Tasks\AfterUserJobPerform;
12
use Resque\Tasks\BeforeUserJobPerform;
13
use Resque\Tasks\BrokenUserJobPerform;
14
use Resque\Tasks\FailedUserJobPerform;
15
16
class Job
17
{
18
    private $queueName;
19
    private $payload;
20
    private $serviceLocator;
21
    private $dispatcher;
22
    private $failed = false;
23
24 7
    public function __construct(string $queueName, array $payload, ContainerInterface $serviceLocator)
25
    {
26 7
        $this->queueName = $queueName;
27 7
        $this->payload = $payload;
28 7
        $this->serviceLocator = $serviceLocator;
29 7
        $this->dispatcher = new Noop();
30 7
    }
31
32 7
    public function setDispatcher(Dispatcher $dispatcher): void
33
    {
34 7
        $this->dispatcher = $dispatcher;
35 7
    }
36
37 3
    public function getPayloadClassName(): string
38
    {
39 3
        if (empty($this->payload['class'])) {
40
            throw new PayloadCorrupt();
41
        }
42 3
        return $this->payload['class'];
43
    }
44
45 3
    public function getPayloadArguments(): array
46
    {
47 3
        if (empty($this->payload['args'])) {
48
            throw new PayloadCorrupt();
49
        }
50 3
        return $this->payload['args'];
51
    }
52
53 7
    public function getQueueName(): string
54
    {
55 7
        return $this->queueName;
56
    }
57
58 7
    public function getPayload(): array
59
    {
60 7
        return $this->payload;
61
    }
62
63 6
    public function hasFailed(): bool
64
    {
65 6
        return $this->failed;
66
    }
67
68 3
    public function perform(): void
69
    {
70 3
        $this->dispatcher->dispatch(BeforeUserJobPerform::class, $this->payload);
71
        try {
72 3
            $userJob = $this->serviceLocator->get($this->getPayloadClassName());
73 3
            $userJob->perform($this->getPayloadArguments());
74 1
            $this->dispatcher->dispatch(AfterUserJobPerform::class, $this->payload);
75 2
        } catch (\Exception $e) {
76 1
            $this->handleFailedJob();
77 1
        } catch (\Error $e) {
78 1
            $this->handleBrokenJob();
79
        }
80 3
    }
81
82 1
    private function handleFailedJob()
83
    {
84 1
        $this->failed = true;
85 1
        $this->dispatcher->dispatch(FailedUserJobPerform::class, $this->payload);
86 1
    }
87
88 1
    private function handleBrokenJob()
89
    {
90 1
        $this->failed = true;
91 1
        $this->dispatcher->dispatch(BrokenUserJobPerform::class, $this->payload);
92 1
    }
93
}
94