Test Failed
Pull Request — master (#9)
by Nicolas
02:36
created

MessageAdapter::getFlags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Puzzle\AMQP\Workers;
4
5
use Psr\Log\InvalidArgumentException;
6
use Puzzle\AMQP\ReadableMessage;
7
use Puzzle\AMQP\Messages\MessageDecoder;
8
9
class MessageAdapter implements ReadableMessage
10
{
11
    private
12
        $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...
13
        $body;
14
15
    public function __construct(\Swarrot\Broker\Message $message)
16
    {
17
        $this->message = $message;
18
        $this->body = (new MessageDecoder())->decode($this);
19
    }
20
21
    public function getRoutingKey()
22
    {
23
        return $this->getAttribute('routing_key');
24
    }
25
26
    public function getContentType()
27
    {
28
        return $this->getAttribute('content_type');
29
    }
30
31
    public function getAppId()
32
    {
33
        return $this->getAttribute('app_id');
34
    }
35
36
    public function getHeaders()
37
    {
38
        return $this->getAttribute('headers');
39
    }
40
41
    public function getDecodedBody()
42
    {
43
        return $this->body->decode();
44
    }
45
46
    public function getRawBody()
47
    {
48
        return $this->message->getBody();
49
    }
50
51
    public function getFlags()
52
    {
53
        throw new \LogicException('Consumed messages have no flags');
54
    }
55
56
    public function getAttribute($attributeName)
57
    {
58
        $messageProperties = $this->message->getProperties();
59
        if(array_key_exists($attributeName, $messageProperties))
60
        {
61
            return $messageProperties[$attributeName];
62
        }
63
64
        throw new InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName));
65
    }
66
67
    public function __toString()
68
    {
69
        return json_encode(array(
70
            'routing_key' => $this->getRoutingKey(),
71
            'body' => (string) $this->body,
72
            'attributes' => $this->message->getProperties(),
73
        ));
74
    }
75
    
76
    public function getService()
77
    {
78
        return $this->getHeader('service');
79
    }
80
81
    public function getAction()
82
    {
83
        return $this->getHeader('action');
84
    }
85
86
    public function getAuthor()
87
    {
88
        return $this->getHeader('author');
89
    }
90
91
    private function getHeader($headerName)
92
    {
93
        $headers = $this->getHeaders();
94
        if(array_key_exists($headerName, $headers))
95
        {
96
            return $headers[$headerName];
97
        }
98
99
        return null;
100
    }
101
102
    public function getAttributes()
103
    {
104
        return $this->message->getProperties();
105
    }
106
107
    public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE)
108
    {
109
        $retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER);
110
111
        return (!empty($retryHeader) && (int) $retryHeader === $retryOccurence);
112
    }
113
114
    public function getRoutingKeyFromHeader()
115
    {
116
        $headers = $this->getHeaders();
117
118
        if(! array_key_exists('routing_key', $headers))
119
        {
120
            return null;
121
        }
122
123
        return $headers['routing_key'];
124
    }
125
}
126