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

RabbitMqJob   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 65
ccs 0
cts 30
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A attempts() 0 4 1
A getRawBody() 0 4 1
A delete() 0 5 1
A release() 0 14 3
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