Test Failed
Pull Request — master (#9)
by Nicolas
03:34
created

MessageAdapter::applyHooks()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 9.2
cc 4
eloc 5
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Puzzle\AMQP\Workers;
4
5
use Puzzle\AMQP\ReadableMessage;
6
use Puzzle\AMQP\Messages\MessageDecoder;
7
8
class MessageAdapter implements ReadableMessage
9
{
10
    private
11
        $message,
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $message.

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...
12
        $body;
13
14
    public function __construct(\Swarrot\Broker\Message $message)
15
    {
16
        $this->message = $message;
17
        $this->body = (new MessageDecoder())->decode($this);
18
    }
19
20
    public function getRoutingKey()
21
    {
22
        return $this->getAttribute('routing_key');
23
    }
24
25
    public function getContentType()
26
    {
27
        return $this->getAttribute('content_type');
28
    }
29
30
    public function getAppId()
31
    {
32
        return $this->getAttribute('app_id');
33
    }
34
35
    public function getHeaders()
36
    {
37
        return $this->getAttribute('headers');
38
    }
39
40
    public function getBody()
41
    {
42
        return $this->body;
43
    }
44
45
    public function getDecodedBody()
46
    {
47
        return $this->body->decode();
48
    }
49
50
    public function getRawBody()
51
    {
52
        return $this->message->getBody();
53
    }
54
55
    public function getFlags()
56
    {
57
        throw new \LogicException('Consumed messages have no flags');
58
    }
59
60
    public function getAttribute($attributeName)
61
    {
62
        $messageProperties = $this->message->getProperties();
63
        if(array_key_exists($attributeName, $messageProperties))
64
        {
65
            return $messageProperties[$attributeName];
66
        }
67
68
        throw new \InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName));
69
    }
70
71
    public function __toString()
72
    {
73
        return json_encode(array(
74
            'routing_key' => $this->getRoutingKey(),
75
            'body' => (string) $this->body,
76
            'attributes' => $this->message->getProperties(),
77
        ));
78
    }
79
80
    private function getHeader($headerName)
81
    {
82
        $headers = $this->getHeaders();
83
        if(array_key_exists($headerName, $headers))
84
        {
85
            return $headers[$headerName];
86
        }
87
88
        return null;
89
    }
90
91
    public function getAttributes()
92
    {
93
        return $this->message->getProperties();
94
    }
95
96
    public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE)
97
    {
98
        $retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER);
99
100
        return ($retryHeader !== null && (int) $retryHeader >= $retryOccurence);
101
    }
102
103
    public function getRoutingKeyFromHeader()
104
    {
105
        return $this->getHeader('routing_key');
106
    }
107
}
108