Completed
Pull Request — master (#14)
by Nicolas
03:03
created

MessageAdapter::getDecodedBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Puzzle\AMQP\Workers;
4
5
use Puzzle\AMQP\ReadableMessage;
6
use Puzzle\AMQP\Messages\BodyFactory;
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 21
    public function __construct(\Swarrot\Broker\Message $message)
15
    {
16 21
        $this->message = $message;
17
        
18 21
        $this->body = (new BodyFactory())->create(
19 21
            $this->getContentType(),
20 20
            $message->getBody()
21 20
        );
22 20
    }
23
24 8
    public function getRoutingKey()
25
    {
26 8
        return $this->getAttribute('routing_key');
27
    }
28
29 21
    public function getContentType()
30
    {
31 21
        return $this->getAttribute('content_type');
32
    }
33
34 1
    public function getAppId()
35
    {
36 1
        return $this->getAttribute('app_id');
37
    }
38
39 16
    public function getHeaders()
40
    {
41 16
        return $this->getAttribute('headers');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getAttribute('headers'); (string) is incompatible with the return type declared by the interface Puzzle\AMQP\MessageMetadata::getHeaders of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42
    }
43
44 4
    public function getBody()
45
    {
46 4
        return $this->body;
47
    }
48
49 2
    public function getBodyInOriginalFormat()
50
    {
51 2
        return $this->body->inOriginalFormat();
52
    }
53
54 21
    public function getAttribute($attributeName)
55
    {
56 21
        $messageProperties = $this->message->getProperties();
57 21
        if(array_key_exists($attributeName, $messageProperties))
58 21
        {
59 20
            return $messageProperties[$attributeName];
60
        }
61
62 2
        throw new \InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName));
63
    }
64
65 3
    public function __toString()
66
    {
67 3
        return json_encode(array(
68 3
            'routing_key' => $this->getRoutingKey(),
69 3
            'body' => (string) $this->body,
70 3
            'attributes' => $this->message->getProperties(),
71 3
        ));
72
    }
73
74 14
    private function getHeader($headerName)
75
    {
76 14
        $headers = $this->getHeaders();
77 14
        if(array_key_exists($headerName, $headers))
78 14
        {
79 12
            return $headers[$headerName];
80
        }
81
82 2
        return null;
83
    }
84
85 3
    public function getAttributes()
86
    {
87 3
        return $this->message->getProperties();
88
    }
89
90 11
    public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE)
91
    {
92 11
        $retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER);
93
94 11
        return ($retryHeader !== null && (int) $retryHeader >= $retryOccurence);
95
    }
96
97 3
    public function getRoutingKeyFromHeader()
98
    {
99 3
        return $this->getHeader('routing_key');
100
    }
101
}
102