Passed
Push — master ( 39b8bd...47b47d )
by Dirk
02:37
created

Job::setDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Interfaces\DispatcherInterface;
10
use Resque\Tasks\AfterUserJobPerform;
11
use Resque\Tasks\BeforeUserJobPerform;
12
use Resque\Tasks\FailedUserJobPerform;
13
14
class Job
15
{
16
    private $queueName;
17
    private $payload;
18
    private $serviceLocator;
19
    private $dispatcher;
20
    private $failed = false;
21
22 3
    public function __construct(string $queueName, array $payload, ContainerInterface $serviceLocator)
23
    {
24 3
        $this->queueName = $queueName;
25 3
        $this->payload = $payload;
26 3
        $this->serviceLocator = $serviceLocator;
27 3
        $this->dispatcher = new Noop();
28 3
    }
29
30 3
    public function setDispatcher(DispatcherInterface $dispatcher): void
31
    {
32 3
        $this->dispatcher = $dispatcher;
33 3
    }
34
35 2
    public function getPayloadClassName(): string
36
    {
37 2
        return $this->payload['class'];
38
    }
39
40 2
    public function getPayloadArguments(): array
41
    {
42 2
        return $this->payload['args'];
43
    }
44
45 3
    public function getQueueName(): string
46
    {
47 3
        return $this->queueName;
48
    }
49
50 3
    public function getPayload(): array
51
    {
52 3
        return $this->payload;
53
    }
54
55 2
    public function hasFailed(): bool
56
    {
57 2
        return $this->failed;
58
    }
59
60 2
    public function perform(): void
61
    {
62 2
        $this->dispatcher->dispatch(BeforeUserJobPerform::class, $this->payload);
63
        try {
64 2
            $userJob = $this->serviceLocator->get($this->getPayloadClassName());
65 2
            $userJob->perform($this->getPayloadArguments());
66 1
            $this->dispatcher->dispatch(AfterUserJobPerform::class, $this->payload);
67 1
        } catch (\Exception $e) {
68 1
            $this->handleFailedJob();
69
        }
70 2
    }
71
72 1
    private function handleFailedJob()
73
    {
74 1
        $this->failed = true;
75 1
        $this->dispatcher->dispatch(FailedUserJobPerform::class, $this->payload);
76 1
    }
77
}
78