Test Failed
Pull Request — master (#9)
by Nicolas
10:03 queued 04:15
created

Message::packHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Puzzle\AMQP\Messages;
4
5
use Psr\Log\InvalidArgumentException;
6
use Puzzle\AMQP\WritableMessage;
7
use Puzzle\AMQP\ReadableMessage;
8
use Puzzle\AMQP\Messages\Bodies\NullBody;
9
10
class Message implements WritableMessage
11
{
12
    use BodySetter;
13
    
14
    protected
15
        $body;
16
    
17
    private
18
        $flags,
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $flags.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
19
        $headers,
20
        $attributes;
21
22
    public function __construct($routingKey = '')
23
    {
24
        $this->body = new NullBody();
25
        $this->flags = AMQP_NOPARAM;
26
        $this->headers = array();
27
        $this->initializeAttributes();
28
        $this->setAttribute('routing_key', $routingKey);
29
    }
30
    
31
    private function initializeAttributes()
32
    {
33
        $this->attributes = array(
34
            'routing_key' => null,
35
            'content_type' => $this->getContentType(),
36
            'content_encoding' => 'utf8',
37
            'message_id' => function($timestamp) {
38
                return sha1($this->getRoutingKey() . $timestamp . $this->body->footprint() . mt_rand());
39
            },
40
            'user_id' => null,
41
            'app_id' => null,
42
            'delivery_mode' => self::PERSISTENT,
43
            'priority' => null,
44
            'timestamp' => function($timestamp) {
45
                return $timestamp;
46
            },
47
            'expiration' => null,
48
            'type' => null,
49
            'reply_to' => null,
50
            'correlation_id' => null,
51
            'headers' => function($timestamp) {
52
                return $this->packHeaders($timestamp);
53
            },
54
        );
55
    }
56
57
    public function getContentType()
58
    {
59
        return $this->body->getContentType();
60
    }
61
62
    public function getRoutingKey()
63
    {
64
        return $this->getAttribute('routing_key');
65
    }
66
67
    public function getFormattedBody()
68
    {
69
        return $this->body->format();
70
    }
71
72
    public function setBody(Body $body)
73
    {
74
        $this->body = $body;
75
        $this->updateContentType();
76
        
77
        return $this;
78
    }
79
    
80
    private function updateContentType()
81
    {
82
        $this->attributes['content_type'] = $this->body->getContentType();
83
    }
84
    
85
    public function getFlags()
86
    {
87
        return $this->flags;
88
    }
89
90
    public function setFlags($flags)
91
    {
92
        $this->flags = $flags;
93
94
        return $this;
95
    }
96
97
    public function addHeader($headerName, $value)
98
    {
99
        $this->headers[$headerName] = $value;
100
101
        return $this;
102
    }
103
104
    public function addHeaders(array $headers)
105
    {
106
        foreach($headers as $name => $value)
107
        {
108
            $this->addHeader($name, $value);
109
        }
110
111
        return $this;
112
    }
113
114
    public function setAuthor($author)
115
    {
116
        $this->addHeader('author', $author);
117
118
        return $this;
119
    }
120
121
    public function packAttributes($timestamp = false)
122
    {
123
        $this->updateContentType();
124
        
125
        if($timestamp === false)
126
        {
127
            $timestamp = (new \DateTime("now"))->getTimestamp();
128
        }
129
130
        return array_map(function($value) use($timestamp) {
131
132
            if($value instanceof \Closure)
133
            {
134
                $value = $value($timestamp);
135
            }
136
137
            return $value;
138
139
        }, $this->attributes);
140
    }
141
142
    private function packHeaders($timestamp)
143
    {
144
        $this->headers['message_datetime'] = date('Y-m-d H:i:s', $timestamp);
145
146
        return $this->headers;
147
    }
148
149
    public function setAttribute($attributeName, $value)
150
    {
151
        if($attributeName !== 'headers')
152
        {
153
            if(array_key_exists($attributeName, $this->attributes))
154
            {
155
                $this->attributes[$attributeName] = $value;
156
            }
157
        }
158
159
        return $this;
160
    }
161
162
    public function getAppId()
163
    {
164
        return $this->getAttribute('app_id');
165
    }
166
167
    public function getHeaders()
168
    {
169
        $attributes = $this->packAttributes();
170
171
        return $attributes['headers'];
172
    }
173
174
    public function getAttribute($attributeName)
175
    {
176
        if(array_key_exists($attributeName, $this->attributes))
177
        {
178
            return $this->attributes[$attributeName];
179
        }
180
181
        throw new InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName));
182
    }
183
184
    public function __toString()
185
    {
186
        return json_encode(array(
187
            'routing_key' => $this->getRoutingKey(),
188
            'body' => (string) $this->body,
189
            'attributes' => $this->attributes,
190
            'flags' => $this->flags
191
        ));
192
    }
193
194
    public function setExpiration($expirationInSeconds)
195
    {
196
        $ttlInMs = 1000 * (int) $expirationInSeconds;
197
198
        $this->setAttribute('expiration', (string) $ttlInMs);
199
200
        return $this;
201
    }
202
203
    public static function buildFromReadableMessage(ReadableMessage $readableMessage, $newRoutingKey = false)
204
    {
205
        $routingKey = $readableMessage->getRoutingKey();
206
207
        if($newRoutingKey !== false)
208
        {
209
            $routingKey = $newRoutingKey;
210
        }
211
212
        $writableMessage = new static($routingKey);
213
        $writableMessage->setBody($readableMessage->getBody());
214
215
        $writableMessage->addHeaders($readableMessage->getHeaders());
216
217
        $attributes = $readableMessage->getAttributes();
218
        $skippedAttributes = array('timestamp', 'headers', 'app_id', 'routing_key');
219
        foreach($attributes as $attributeName => $value)
220
        {
221
            if(! in_array($attributeName, $skippedAttributes))
222
            {
223
                $writableMessage->setAttribute($attributeName, $value);
224
            }
225
        }
226
227
        return $writableMessage;
228
    }
229
}
230