Completed
Push — master ( d80ef0...22d605 )
by Cody
02:27
created

RabbitMqJob::fire()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Ccovey\LaravelRabbitMQ;
4
5
use Ccovey\RabbitMQ\Producer\Message;
6
use Ccovey\RabbitMQ\Producer\Producer;
7
use Ccovey\RabbitMQ\Producer\ProducerInterface;
8
use Ccovey\RabbitMQ\QueuedMessage;
9
use DateTime;
10
use Illuminate\Contracts\Container\Container;
11
use Illuminate\Contracts\Queue\Job as JobContract;
12
use Illuminate\Queue\Jobs\Job;
13
14
class RabbitMqJob extends Job implements JobContract
15
{
16
    /**
17
     * @var Container
18
     */
19
    protected $container;
20
21
    /**
22
     * @var ProducerInterface
23
     */
24
    private $producer;
25
26
    /**
27
     * @var QueuedMessage
28
     */
29
    private $message;
30
31
    public function __construct(Container $container, ProducerInterface $producer, QueuedMessage $message)
32
    {
33
        $this->container = $container;
34
        $this->producer = $producer;
35
        $this->message = $message;
36
    }
37
38
    /**
39
     * Get the number of times the job has been attempted.
40
     *
41
     * @return int
42
     */
43
    public function attempts()
44
    {
45
        return $this->message->attempts ?? 0;
46
    }
47
48
    /**
49
     * Get the raw body string for the job.
50
     *
51
     * @return string
52
     */
53
    public function getRawBody()
54
    {
55
        $this->message->getRawBody();
0 ignored issues
show
Bug introduced by
The method getRawBody() does not seem to exist on object<Ccovey\RabbitMQ\QueuedMessage>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
    }
57
58
    public function delete()
59
    {
60
        parent::delete();
61
        $this->producer->getChannel()->acknowledge($this->message->delivery_tag);
62
    }
63
64
    public function release($delay = 0)
65
    {
66
        parent::release($delay);
67
        $body = $this->message->getBody();
68
69
        if ($delay > 0) {
70
            $body['scheduledAt'] = new DateTime(sprintf('+%s', $delay));
71
        }
72
73
        $body['attempts'] = isset($body['attempts']) ? $body['attempts'] + 1 : 1;
74
        $message = new Message($body, $this->message->getQueueName());
75
76
        $this->producer->publish($message);
77
    }
78
}
79