Passed
Push — master ( c65021...b58a23 )
by Murilo
02:50
created

RabbitMQ   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A closeConnection() 0 8 3
A __destruct() 0 3 1
A getInstance() 0 21 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Source\Infra\Queue\RabbitMQ;
6
7
use PhpAmqpLib\Connection\AMQPStreamConnection;
0 ignored issues
show
Bug introduced by
The type PhpAmqpLib\Connection\AMQPStreamConnection 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...
8
use PhpAmqpLib\Exchange\AMQPExchangeType;
0 ignored issues
show
Bug introduced by
The type PhpAmqpLib\Exchange\AMQPExchangeType 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...
9
10
use Source\Infra\Queue\RabbitMQ\RabbitMQInterface;
11
12
/**
13
 * RabbitMQ class...
14
 * 
15
 * @version 0.1.0
16
 */
17
abstract class RabbitMQ implements RabbitMQInterface
18
{
19
    use RabbitTrait;
20
21
    /** @var string $host */
22
    const HOST = RABBITMQ_HOST;
23
24
    /** @var int $port */
25
    const PORT = RABBITMQ_PORT;
26
27
    /** @var string $user */
28
    const USER = RABBITMQ_USER;
29
30
    /** @var string $passwd */
31
    const PASSWD = RABBITMQ_PASS;
32
    
33
    /** @var AMQPStreamConnection $connection */
34
    protected $connection;
35
36
    /** @var object $channel */
37
    protected $channel;
38
39
    /**
40
     * Declare the queue and exchanger of the your rabbitmq server
41
     *
42
     * @param string $queue
43
     * @param string $exchanger
44
     * @param bool $reply
45
     * @return self
46
     */
47
    public function getInstance(string $queue, string $exchanger, bool $reply = false): self
48
    {
49
        // basic validation for the queue, valid by the trait
50
        $this->setQueue($queue);
51
52
        // basic validation for the exchanger, valid by the trait
53
        $this->setExchanger($exchanger);
54
55
        // basic validation for the reply, valid by the trait
56
        $this->setReply($reply);
57
58
        $this->connection = new AMQPStreamConnection(self::HOST, self::PORT, self::USER, self::PASSWD);
59
        $this->channel = $this->connection->channel();
60
61
        $this->channel->queue_declare($this->queue, true, false, false);
62
        $this->channel->exchange_declare($this->exchanger, AMQPExchangeType::DIRECT, false, true, false);
63
        $this->channel->queue_bind($this->queue, $this->exchanger);
64
65
        
66
        $this->channel->basic_qos(null, 1, null);
67
        return $this;
68
    }
69
70
    /**
71
     * Close the connection with the rabbitmq
72
     *
73
     * @return void
74
     */
75
    public function closeConnection(): void
76
    {
77
        if (!empty($this->channel)) {
78
            $this->channel->close();
79
        }
80
        
81
        if (!empty($this->connection)) {
82
            $this->connection->close();
83
        }
84
    }
85
86
    /**
87
     * Close the connection with the rabbitmq
88
     *
89
     * @return void
90
     */
91
    public function __destruct()
92
    {
93
        $this->closeConnection();
94
    }
95
}
96