Passed
Push — master ( f047b9...ae8494 )
by Sergey
08:39
created

Delivery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 16
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace ButterAMQP;
4
5
/**
6
 * Delivery is a message received (consumed) from the server.
7
 */
8
class Delivery extends Message
9
{
10
    /**
11
     * @var ChannelInterface
12
     */
13
    private $channel;
14
15
    /**
16
     * @var string
17
     */
18
    private $consumerTag;
19
20
    /**
21
     * @var string
22
     */
23
    private $deliveryTag;
24
25
    /**
26
     * @var bool
27
     */
28
    private $redeliver;
29
30
    /**
31
     * @var string
32
     */
33
    private $exchange;
34
35
    /**
36
     * @var string
37
     */
38
    private $routingKey;
39
40
    /**
41
     * @param ChannelInterface $channel
42
     * @param string           $consumerTag
43
     * @param string           $deliveryTag
44
     * @param bool             $redeliver
45
     * @param string           $exchange
46
     * @param string           $routingKey
47
     * @param string           $body
48
     * @param array            $properties
49
     */
50 16
    public function __construct(
51
        ChannelInterface $channel,
52
        $consumerTag,
53
        $deliveryTag,
54
        $redeliver,
55
        $exchange,
56
        $routingKey,
57
        $body,
58
        array $properties
59
    ) {
60 16
        $this->channel = $channel;
61 16
        $this->consumerTag = $consumerTag;
62 16
        $this->deliveryTag = $deliveryTag;
63 16
        $this->redeliver = $redeliver;
64 16
        $this->exchange = $exchange;
65 16
        $this->routingKey = $routingKey;
66
67 16
        parent::__construct($body, $properties);
68 16
    }
69
70
    /**
71
     * Acknowledge message, marking it as one successfully processed by consumer.
72
     *
73
     * @param bool $multiple
74
     *
75
     * @return $this
76
     */
77 6
    public function ack($multiple = false)
78
    {
79 6
        $this->channel->ack($this->deliveryTag, $multiple);
80
81 6
        return $this;
82
    }
83
84
    /**
85
     * Reject message(s) marking it as one which consumer fail to process.
86
     *
87
     * @param bool $requeue  makes AMQP server put messages back to the queue
88
     * @param bool $multiple reject all delivered and not acknowledged messages including current one
89
     *
90
     * @return $this
91
     */
92 3
    public function reject($requeue = true, $multiple = false)
93
    {
94 3
        $this->channel->reject($this->deliveryTag, $requeue, $multiple);
95
96 3
        return $this;
97
    }
98
99
    /**
100
     * Cancel message consuming.
101
     *
102
     * @return $this
103
     */
104 6
    public function cancel()
105
    {
106 6
        if (empty($this->consumerTag)) {
107 1
            throw new \LogicException('Consumer is not assigned to this delivery.');
108
        }
109
110 5
        $this->channel->cancel($this->consumerTag);
111
112 5
        return $this;
113
    }
114
115
    /**
116
     * Consume tag - unique identifier of the consumer within a channel.
117
     * Used to identify consumer in frames related to it, like basic.cancel.
118
     *
119
     * @return string
120
     */
121 2
    public function getConsumerTag()
122
    {
123 2
        return $this->consumerTag;
124
    }
125
126
    /**
127
     * Delivery tag - unique identifier of the delivery within a channel.
128
     * Used to identify delivery in frames related to it, like basic.ack or basic.reject.
129
     *
130
     * @return string
131
     */
132 2
    public function getDeliveryTag()
133
    {
134 2
        return $this->deliveryTag;
135
    }
136
137
    /**
138
     * Redeliver is true if message was rejected before with re-enqueue set to true.
139
     *
140
     * @return bool
141
     */
142 2
    public function isRedeliver()
143
    {
144 2
        return $this->redeliver;
145
    }
146
147
    /**
148
     * Exchange where message was sent initially.
149
     *
150
     * @return string
151
     */
152 3
    public function getExchange()
153
    {
154 3
        return $this->exchange;
155
    }
156
157
    /**
158
     * Routing key.
159
     *
160
     * @return string
161
     */
162 3
    public function getRoutingKey()
163
    {
164 3
        return $this->routingKey;
165
    }
166
167
    /**
168
     * Define how to print object when dumping.
169
     *
170
     * @return array
171
     */
172 1
    public function __debugInfo()
173
    {
174 1
        return array_merge(parent::__debugInfo(), [
175 1
            'consumer_tag' => $this->consumerTag,
176 1
            'delivery_tag' => $this->deliveryTag,
177 1
            'redeliver' => $this->redeliver,
178 1
            'exchange' => $this->exchange,
179 1
            'routing_key' => $this->routingKey,
180 1
            'channel_object_hash' => spl_object_hash($this->channel),
181 1
        ]);
182
    }
183
}
184