Passed
Pull Request — master (#12)
by frey
02:26
created

CMQJob::payload()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Freyo\LaravelQueueCMQ\Queue\Jobs;
4
5
use Freyo\LaravelQueueCMQ\Queue\CMQQueue;
6
use Freyo\LaravelQueueCMQ\Queue\Driver\Message;
7
use Freyo\LaravelQueueCMQ\Queue\Driver\Queue;
8
use Illuminate\Container\Container;
9
use Illuminate\Contracts\Queue\Job as JobContract;
10
use Illuminate\Queue\Jobs\Job;
11
12
class CMQJob extends Job implements JobContract
13
{
14
    protected $connection;
15
    protected $message;
16
17
    public function __construct(Container $container, CMQQueue $connection, Message $message, Queue $queue)
18
    {
19
        $this->container = $container;
20
        $this->connection = $connection;
21
        $this->message = $message;
22
        $this->queue = $queue->getQueueName();
23
    }
24
25
    /**
26
     * Get the job identifier.
27
     *
28
     * @return string
29
     */
30
    public function getJobId()
31
    {
32
        return $this->message->msgId;
33
    }
34
35
    /**
36
     * Get the raw body of the job.
37
     *
38
     * @return string
39
     */
40
    public function getRawBody()
41
    {
42
        return $this->message->msgBody;
43
    }
44
45
    /**
46
     * Get the number of times the job has been attempted.
47
     *
48
     * @return int
49
     */
50
    public function attempts()
51
    {
52
        return $this->message->dequeueCount;
53
    }
54
55
    /**
56
     * Fire the job.
57
     *
58
     * @return void
59
     */
60
    public function fire()
61
    {
62
        method_exists($this, 'resolveAndFire')
63
            ? $this->resolveAndFire($this->payload())
64
            : parent::fire();
65
    }
66
67
    /**
68
     * Get the decoded body of the job.
69
     *
70
     * @return array
71
     */
72
    public function payload()
73
    {
74
        if ($this->connection->isPlain()) {
75
76
            $job = $this->connection->getPlainJob();
77
78
            return [
79
                'displayName' => is_string($job) ? explode('@', $job)[0] : null,
0 ignored issues
show
introduced by
The condition is_string($job) is always true.
Loading history...
80
                'job' => $job,
81
                'maxTries' => null,
82
                'timeout' => null,
83
                'data' => $this->getRawBody(),
84
            ];
85
        }
86
87
        return json_decode($this->getRawBody(), true);
88
    }
89
90
    /**
91
     * Delete the job from the queue.
92
     *
93
     * @return void
94
     */
95
    public function delete()
96
    {
97
        parent::delete();
98
99
        $this->connection->getQueue($this->getQueue())->delete_message($this->message->receiptHandle);
100
    }
101
102
    /**
103
     * Release the job back into the queue.
104
     *
105
     * @param int $delay
106
     *
107
     * @return void
108
     */
109
    public function release($delay = 0)
110
    {
111
        parent::release($delay);
112
    }
113
}
114