Completed
Pull Request — master (#9)
by Nicolas
04:39
created

Message::getAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $body.

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