Passed
Branch master (74fab5)
by Kunal
03:19
created

RabbitMQDelivery   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 90
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A reject() 0 19 2
A setConfig() 0 5 1
A acknowledge() 0 24 3
A __construct() 0 3 1
A getConfig() 0 3 1
1
<?php
2
3
namespace Kunnu\RabbitMQ;
4
5
use Illuminate\Support\Collection;
6
use PhpAmqpLib\Channel\AMQPChannel;
7
8
class RabbitMQDelivery
9
{
10
    protected Collection $config;
11
12
    public function __construct(array $config = [])
13
    {
14
        $this->setConfig($config);
15
    }
16
17
    /**
18
     * @return Collection
19
     */
20
    public function getConfig(): Collection
21
    {
22
        return $this->config;
23
    }
24
25
    /**
26
     * @param array $config
27
     *
28
     * @return \Anik\Amqp\Delivery
0 ignored issues
show
Bug introduced by
The type Anik\Amqp\Delivery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     */
30
    public function setConfig(array $config): self
31
    {
32
        $this->config = new Collection($config);
33
34
        return $this;
35
    }
36
37
    /**
38
     * Acknowledge a message.
39
     *
40
     * @throws RabbitMQException
41
     * @return bool
42
     *
43
     */
44
    public function acknowledge(): bool
45
    {
46
        $config = $this->getConfig();
47
        $info = $config->get('delivery_info', []);
48
        /**
49
         * @var AMQPChannel
50
         */
51
        $channel = $info['channel'] ?? null;
52
53
        if (!$channel) {
54
            throw new RabbitMQException('Delivery info or channel is not set');
55
        }
56
57
        $channel->basic_ack(
58
            $info['delivery_tag'] ?? null
59
        );
60
61
        if ($config->get('body') === 'quit') {
62
            $channel->basic_cancel(
63
                $info['consumer_tag'] ?? null
64
            );
65
        }
66
67
        return true;
68
    }
69
70
    /**
71
     * Rejects message w/ requeue.
72
     *
73
     * @param bool $requeue
74
     *
75
     * @throws RabbitMQException
76
     * @return bool
77
     *
78
     */
79
    public function reject($requeue = false): bool
80
    {
81
        $config = $this->getConfig();
82
        $info = $config->get('delivery_info');
83
        /**
84
         * @var AMQPChannel
85
         */
86
        $channel = $info['channel'] ?? null;
87
88
        if (!$channel) {
89
            throw new RabbitMQException('Delivery info or channel is not set');
90
        }
91
92
        $channel->basic_reject(
93
            $info['delivery_tag'] ?? null,
94
            $requeue
95
        );
96
97
        return true;
98
    }
99
}
100