Completed
Push — master ( f58be8...855521 )
by Tilita
03:07
created

ExchangeEntity::bind()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 7.4822

Importance

Changes 0
Metric Value
dl 22
loc 22
ccs 11
cts 14
cp 0.7856
rs 8.6346
c 0
b 0
f 0
cc 7
nc 5
nop 0
crap 7.4822
1
<?php
2
namespace NeedleProject\LaravelRabbitMq\Entity;
3
4
use NeedleProject\LaravelRabbitMq\AMQPConnection;
5
use NeedleProject\LaravelRabbitMq\PublisherInterface;
6
use PhpAmqpLib\Channel\AMQPChannel;
7
use PhpAmqpLib\Exception\AMQPProtocolChannelException;
8
use PhpAmqpLib\Message\AMQPMessage;
9
10
/**
11
 * Class ExchangeEntity
12
 *
13
 * @package NeedleProject\LaravelRabbitMq\Entity
14
 * @author  Adrian Tilita <[email protected]>
15
 */
16
class ExchangeEntity implements PublisherInterface, AMQPEntityInterface
17
{
18
    /**
19
     * @const array Default connections parameters
20
     */
21
    const DEFAULTS = [
22
        'exchange_type'                => 'topic',
23
        'passive'                      => false,
24
        'durable'                      => false,
25
        'auto_delete'                  => false,
26
        'internal'                     => false,
27
        'nowait'                       => false,
28
        'auto_create'                  => false,
29
        'throw_exception_on_redeclare' => true,
30
        'throw_exception_on_bind_fail' => true,
31
    ];
32
33
    /**
34
     * @var AMQPConnection
35
     */
36
    protected $connection;
37
38
    /**
39
     * @var string
40
     */
41
    protected $aliasName;
42
43
    /**
44
     * @var array
45
     */
46
    protected $attributes;
47
48
    /**
49
     * ExchangeEntity constructor.
50
     *
51
     * @param AMQPConnection $connection
52
     * @param string $aliasName
53
     * @param array $attributes
54
     */
55 13
    public function __construct(AMQPConnection $connection, string $aliasName, array $attributes = [])
56
    {
57 13
        $this->connection = $connection;
58 13
        $this->aliasName  = $aliasName;
59 13
        $this->attributes = $attributes;
60 13
    }
61
62
    /**
63
     * @param AMQPConnection $connection
64
     * @param string $aliasName
65
     * @param array $exchangeDetails
66
     * @return ExchangeEntity
67
     */
68 13
    public static function createExchange(AMQPConnection $connection, string $aliasName, array $exchangeDetails)
69
    {
70 13
        return new static(
71
            $connection,
72
            $aliasName,
73 13
            array_merge(self::DEFAULTS, $exchangeDetails)
74
        );
75
    }
76
77
    /**
78
     * @return string
79
     */
80 2
    public function getAliasName(): string
81
    {
82 2
        return $this->aliasName;
83
    }
84
85
    /**
86
     * @return AMQPConnection
87
     */
88 8
    protected function getConnection(): AMQPConnection
89
    {
90 8
        return $this->connection;
91
    }
92
93
    /**
94
     * @return AMQPChannel
95
     */
96 8
    protected function getChannel(): AMQPChannel
97
    {
98 8
        return $this->getConnection()->getChannel();
99
    }
100
101
    /**
102
     * Create the Queue
103
     */
104 4 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        try {
107 4
            $this->getChannel()
108 4
                ->exchange_declare(
109 4
                    $this->attributes['name'],
110 4
                    $this->attributes['exchange_type'],
111 4
                    $this->attributes['passive'],
112 4
                    $this->attributes['durable'],
113 4
                    $this->attributes['auto_delete'],
114 4
                    $this->attributes['internal'],
115 4
                    $this->attributes['nowait']
116
                );
117 2
        } catch (AMQPProtocolChannelException $e) {
118
            // 406 is a soft error triggered for precondition failure (when redeclaring with different parameters)
119 2
            if (true === $this->attributes['throw_exception_on_redeclare'] || $e->amqp_reply_code !== 406) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $e->amqp_reply_code (string) and 406 (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
120 1
                throw $e;
121
            }
122
            // a failure trigger channels closing process
123 1
            $this->getConnection()->reconnect();
124
        }
125 3
    }
126
127
    /**
128
     * @throws AMQPProtocolChannelException
129
     */
130 3 View Code Duplication
    public function bind()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132 3
        if (!isset($this->attributes['bind']) || empty($this->attributes['bind'])) {
133 1
            return;
134
        }
135 2
        foreach ($this->attributes['bind'] as $bindItem) {
136
            try {
137 2
                $this->getChannel()
138 2
                    ->queue_bind(
139 2
                        $bindItem['queue'],
140 2
                        $this->attributes['name'],
141 2
                        $bindItem['routing_key']
142
                    );
143
            } catch (AMQPProtocolChannelException $e) {
144
                // 404 is the code for trying to bind to an non-existing entity
145
                if (true === $this->attributes['throw_exception_on_bind_fail'] || $e->amqp_reply_code !== 404) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $e->amqp_reply_code (string) and 404 (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
146
                    throw $e;
147
                }
148 2
                $this->getConnection()->reconnect();
149
            }
150
        }
151 2
    }
152
153
    /**
154
     * Delete the queue
155
     */
156 1
    public function delete()
157
    {
158 1
        $this->getChannel()->exchange_delete($this->attributes['name']);
159 1
    }
160
161
    /**
162
     * Publish a message
163
     *
164
     * @param string $message
165
     * @param string $routingKey
166
     * @return mixed|void
167
     * @throws AMQPProtocolChannelException
168
     */
169 3 View Code Duplication
    public function publish(string $message, string $routingKey = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171 3
        if ($this->attributes['auto_create'] === true) {
172 1
            $this->create();
173 1
            $this->bind();
174
        }
175 3
        $this->getChannel()->basic_publish(
176 3
            new AMQPMessage($message),
177 3
            $this->attributes['name'],
178
            $routingKey,
179 3
            true
180
        );
181 3
    }
182
}
183