Completed
Push — master ( 65967c...597be2 )
by Cody
02:06
created

Channel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 20.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 31
loc 153
ccs 68
cts 76
cp 0.8947
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A wait() 0 4 1
A acknowledge() 0 4 1
A nack() 0 4 1
A consume() 0 16 1
A publish() 0 19 2
A basicQos() 0 6 1
A declareQueue() 15 15 1
A bindQueue() 0 13 1
A declareExchange() 16 16 1
A getCallbacks() 0 4 1
A setCallbacks() 0 4 1
A getMessage() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ccovey\RabbitMQ;
4
5
use Ccovey\RabbitMQ\Consumer\Consumable;
6
use Ccovey\RabbitMQ\Producer\Publishable;
7
use PhpAmqpLib\Channel\AMQPChannel;
8
use PhpAmqpLib\Message\AMQPMessage;
9
10
class Channel implements ChannelInterface
11
{
12
    /**
13
     * @var AMQPChannel
14
     */
15
    private $channel;
16
17 14
    public function __construct(AMQPChannel $channel)
18
    {
19 14
        $this->channel = $channel;
20 14
    }
21
22 2
    public function wait()
23
    {
24 2
        return $this->channel->wait();
25
    }
26
27 1
    public function acknowledge(string $deliveryTag, bool $multiple = false)
28
    {
29 1
        return $this->channel->basic_ack($deliveryTag, $multiple);
30
    }
31
32 3
    public function nack(string $deliveryTag, bool $multiple = false, bool $requeue = false)
33
    {
34 3
        return $this->channel->basic_nack($deliveryTag, $multiple, $requeue);
35
    }
36
37 2
    public function consume(Consumable $consumable) : ChannelInterface
38
    {
39 2
        $this->channel->basic_consume(
40 2
            $consumable->getQueueName(),
41 2
            $consumable->getConsumerTag(),
42 2
            $consumable->isNoLocal(),
43 2
            $consumable->isNoAck(),
44 2
            $consumable->isExclusive(),
45 2
            $consumable->isNoWait(),
46 2
            $consumable->getCallback(),
47 2
            $consumable->getTicket(),
48 2
            $consumable->getArguments()
49
        );
50
51 2
        return $this;
52
    }
53
54 2
    public function publish(Publishable $message) : ChannelInterface
55
    {
56 2
        if ($message->isDelayed()) {
57
            $queue = new Queue($message->getQueueName(), $message->getDelayedExchanged());
58
            $message->setExchange($message->getDelayedExchanged());
0 ignored issues
show
Bug introduced by
The method setExchange() does not seem to exist on object<Ccovey\RabbitMQ\Producer\Publishable>.

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...
59
            $this->bindQueue($queue);
60
        }
61
62 2
        $this->channel->basic_publish(
63 2
            $message->getMessage(),
64 2
            $message->getExchange(),
65 2
            $message->getQueueName(),
66 2
            $message->isMandatory(),
67 2
            $message->isImmediate(),
68 2
            $message->getTicket()
69
        );
70
71 2
        return $this;
72
    }
73
74
    /**
75
     * @param     $prefetchSize
76
     * @param int $prefetchCount
77
     * @param     $aGlobal
78
     *
79
     * @return mixed
80
     */
81 1
    public function basicQos($prefetchSize = null, int $prefetchCount = 1, $aGlobal = null)
82
    {
83 1
        $this->channel->basic_qos($prefetchSize, $prefetchCount, $aGlobal);
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * @param Queue $queue
90
     *
91
     * @return ChannelInterface
92
     */
93 2 View Code Duplication
    public function declareQueue(Queue $queue) : ChannelInterface
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...
94
    {
95 2
        $this->channel->queue_declare(
96 2
            $queue->getQueueName(),
97 2
            $queue->isPassive(),
98 2
            $queue->isDurable(),
99 2
            $queue->isExclusive(),
100 2
            $queue->isAutoDelete(),
101 2
            $queue->isNoWait(),
102 2
            $queue->getArguments(),
0 ignored issues
show
Bug introduced by
It seems like $queue->getArguments() targeting Ccovey\RabbitMQ\Queue::getArguments() can also be of type object<PhpAmqpLib\Wire\AMQPTable>; however, PhpAmqpLib\Channel\AMQPChannel::queue_declare() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103 2
            $queue->getTicket()
104
        );
105
106 2
        return $this;
107
    }
108
109 2
    public function bindQueue(Queue $queue) : ChannelInterface
110
    {
111 2
        $this->channel->queue_bind(
112 2
            $queue->getQueueName(),
113 2
            $queue->getExchange(),
114 2
            $queue->getQueueName(),
115 2
            $queue->isNoWait(),
116 2
            $queue->getArguments(),
0 ignored issues
show
Bug introduced by
It seems like $queue->getArguments() targeting Ccovey\RabbitMQ\Queue::getArguments() can also be of type object<PhpAmqpLib\Wire\AMQPTable>; however, PhpAmqpLib\Channel\AMQPChannel::queue_bind() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
117 2
            $queue->getTicket()
118
        );
119
120 2
        return $this;
121
    }
122
123 2 View Code Duplication
    public function declareExchange(Exchange $exchange) : ChannelInterface
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...
124
    {
125 2
        $this->channel->exchange_declare(
126 2
            $exchange->getExchange(),
127 2
            $exchange->getType(),
128 2
            $exchange->isPassive(),
129 2
            $exchange->isDurable(),
130 2
            $exchange->isAutoDelete(),
131 2
            $exchange->isInternal(),
132 2
            $exchange->isNoWait(),
133 2
            $exchange->getArguments(),
134 2
            $exchange->getTicket()
135
        );
136
137 2
        return $this;
138
    }
139
140
    /**
141
     * @return array
142
     */
143 2
    public function getCallbacks() : array
144
    {
145 2
        return $this->channel->callbacks;
146
    }
147
148
    public function setCallbacks(array $callbacks = [])
149
    {
150
        $this->channel->callbacks = $callbacks;
151
    }
152
153
    /**
154
     * @param Consumable $consumable
155
     *
156
     * @return AMQPMessage|null
157
     */
158
    public function getMessage(Consumable $consumable)
159
    {
160
        return $this->channel->basic_get($consumable->getQueueName(), $consumable->isNoAck(), $consumable->getTicket());
161
    }
162
}
163