RabbitMqJob::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Ccovey\LaravelRabbitMQ;
4
5
use Ccovey\RabbitMQ\Consumer\ConsumerInterface;
6
use Ccovey\RabbitMQ\Producer\Message;
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 ConsumerInterface
28
     */
29
    private $consumer;
30
31
    /**
32
     * @var QueuedMessage
33
     */
34
    private $message;
35
36
    public function __construct(Container $container, ProducerInterface $producer, ConsumerInterface $consumer, QueuedMessage $message)
37
    {
38
        $this->container = $container;
39
        $this->producer = $producer;
40
        $this->consumer = $consumer;
41
        $this->message = $message;
42
        $this->queue = $this->message->getQueueName();
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->attempts ?? 0;
53
    }
54
55
    /**
56
     * Get the raw body string for the job.
57
     *
58
     * @return string
59
     */
60
    public function getRawBody()
61
    {
62
        return $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...
63
    }
64
65
    public function delete()
66
    {
67
        parent::delete();
68
        $this->consumer->getChannel()->acknowledge($this->message->getDeliveryTag());
0 ignored issues
show
Bug introduced by
The method getDeliveryTag() 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...
69
    }
70
71
    public function release($delay = 0)
72
    {
73
        parent::release($delay);
74
        $body = $this->message->getBody();
75
76
        if ($delay > 0) {
77
            $body['scheduledAt'] = new DateTime(sprintf('+%s', $delay));
78
        }
79
80
        $body['attempts'] = isset($body['attempts']) ? $body['attempts'] + 1 : 1;
81
        $message = new Message($body, $this->message->getQueueName());
82
83
        $this->producer->publish($message);
84
        $this->consumer->getChannel()->acknowledge($this->message->getDeliveryTag());
0 ignored issues
show
Bug introduced by
The method getDeliveryTag() 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...
85
    }
86
}
87