Passed
Pull Request — master (#9)
by Nicolas
03:16
created

MessageAdapter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 2
cbo 2
dl 0
loc 100
ccs 45
cts 45
cp 1
rs 10
c 1
b 0
f 0

15 Methods

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