Completed
Push — master ( 19eed2...9f400a )
by Cody
02:36
created

Message::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 18
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 Ccovey\RabbitMQ\Producer;
4
5
use DateTime;
6
use PhpAmqpLib\Message\AMQPMessage;
7
use PhpAmqpLib\Wire\AMQPTable;
8
9
class Message implements Publishable
10
{
11
    /**
12
     * @var array
13
     */
14
    private $body;
15
16
    /**
17
     * @var string
18
     */
19
    private $queueName;
20
21
    /**
22
     * @var AMQPMessage
23
     */
24
    private $message;
25
26
    /**
27
     * @var string
28
     */
29
    private $exchange;
30
31
    /**
32
     * @var bool
33
     */
34
    private $mandatory;
35
36
    /**
37
     * @var bool
38
     */
39
    private $immediate;
40
41
    /**
42
     * @var int
43
     */
44
    private $ticket;
45
46
    /**
47
     * @var array
48
     */
49
    private $properties;
50
51
    /**
52
     * @var int
53
     */
54
    private $deliveryMode;
55
56
    /**
57
     * @var string
58
     */
59
    private $delayedExchange;
60
61 3
    public function __construct(
62
        array $body,
63
        string $queueName,
64
        string $exchange = '',
65
        bool $mandatory = false,
66
        bool $immediate = false,
67
        int $ticket = null,
68
        array $properties = [],
69
        int $deliveryMode = AMQPMessage::DELIVERY_MODE_PERSISTENT
70
    ) {
71 3
        $this->body = $body;
72 3
        $this->queueName = $queueName;
73 3
        $this->exchange = $exchange;
74 3
        $this->mandatory = $mandatory;
75 3
        $this->immediate = $immediate;
76 3
        $this->ticket = $ticket;
77 3
        $this->deliveryMode = $deliveryMode;
78 3
        $this->properties = $this->buildProperties($properties);
79
80 3
        $this->message = new AMQPMessage(json_encode($body), $this->properties);
81 3
    }
82
83
    /**
84
     * @return bool
85
     */
86 3
    public function isDelayed() : bool
87
    {
88 3
        $now = new \DateTime();
89
90 3
        return isset($this->body['scheduledAt'])
91 3
               && $this->body['scheduledAt'] instanceof DateTime
92 3
               && $now < $this->body['scheduledAt'];
93
    }
94
95
    public function getScheduledAtInSeconds() : int
96
    {
97
        $now = new \DateTime();
98
99
        return $this->body['scheduledAt']->getTimestamp() - $now->getTimestamp() * 1000;
100
    }
101
102 2
    public function getMessage() : AMQPMessage
103
    {
104 2
        return $this->message;
105
    }
106
107 3
    private function buildProperties(array $properties) : array
108
    {
109
        // I don't love this now that it is out of OpenSky. May need to be smarter.
110 3
        if ($this->isDelayed()) {
111
            if (!isset($properties['application_headers'])) {
112
                $properties['application_headers'] = new AMQPTable(['x-delay' => $this->getScheduledAtInSeconds()]);
113
            } else {
114
                $properties['application_headers']->set('x-delay', $this->getScheduledAtInSeconds());
115
            }
116
        }
117
118 3
        $properties['delivery_mode'] = $this->deliveryMode;
119
120 3
        return $properties;
121
    }
122
123
    public function getBody() : array
124
    {
125
        return $this->body;
126
    }
127
128 2
    public function getQueueName() : string
129
    {
130 2
        return $this->queueName;
131
    }
132
133 2
    public function getExchange() : string
134
    {
135 2
        return $this->exchange;
136
    }
137
138 2
    public function isMandatory() : bool
139
    {
140 2
        return $this->mandatory;
141
    }
142
143 2
    public function isImmediate() : bool
144
    {
145 2
        return $this->immediate;
146
    }
147
148 2
    public function getTicket()
149
    {
150 2
        return $this->ticket;
151
    }
152
153
    public function getProperties() : array
154
    {
155
        return $this->properties;
156
    }
157
158
    public function getDeliveryMode() : int
159
    {
160
        return $this->deliveryMode;
161
    }
162
163
    /**
164
     * @param int $deliveryMode
165
     */
166 1
    public function setDeliveryMode(int $deliveryMode)
167
    {
168 1
        $this->deliveryMode = $deliveryMode;
169 1
        $this->properties['delivery_mode'] = $this->deliveryMode;
170 1
    }
171
172
    public function getDelayedExchanged() : string
173
    {
174
        return $this->delayedExchange;
175
    }
176
}
177