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

MessageAdapter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

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
    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