Completed
Push — master ( 62df29...4bf91f )
by Nicolas
02:57
created

Raw::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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