Completed
Push — master ( ad9f01...c2c3ec )
by frey
15s
created

CMQJob::release()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
            $job = $this->connection->getPlainJob();
76
77
            return [
78
                'displayName' => is_string($job) ? explode('@', $job)[0] : null,
0 ignored issues
show
introduced by
The condition is_string($job) is always true.
Loading history...
79
                'job'         => $job,
80
                'maxTries'    => null,
81
                'timeout'     => null,
82
                'data'        => $this->getRawBody(),
83
            ];
84
        }
85
86
        return json_decode($this->getRawBody(), true);
87
    }
88
89
    /**
90
     * Delete the job from the queue.
91
     *
92
     * @return void
93
     */
94
    public function delete()
95
    {
96
        parent::delete();
97
98
        $this->connection->getQueue($this->getQueue())->delete_message($this->message->receiptHandle);
99
    }
100
101
    /**
102
     * Release the job back into the queue.
103
     *
104
     * @param int $delay
105
     *
106
     * @return void
107
     */
108
    public function release($delay = 0)
109
    {
110
        parent::release($delay);
111
    }
112
}
113