Passed
Pull Request — master (#14)
by Nicolas
02:45
created

MessageAdapter::getHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Puzzle\AMQP\Workers;
4
5
use Puzzle\AMQP\ReadableMessage;
6
use Puzzle\AMQP\Messages\BodyFactory;
7
use Puzzle\AMQP\WritableMessage;
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 21
    public function __construct(\Swarrot\Broker\Message $message)
16
    {
17 21
        $this->message = $message;
18
        
19 21
        $this->body = (new BodyFactory())->create(
20 21
            $this->getContentType(),
21 20
            $message->getBody()
22 20
        );
23 20
    }
24
25 7
    public function getRoutingKey()
26
    {
27 7
        return $this->getAttribute('routing_key');
28
    }
29
30 21
    public function getContentType()
31
    {
32 21
        return $this->getAttribute('content_type');
33
    }
34
35 1
    public function getAppId()
36
    {
37 1
        return $this->getAttribute('app_id');
38
    }
39
40 16
    public function getHeaders()
41
    {
42 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...
43
    }
44
45 2
    public function getBodyInOriginalFormat()
46
    {
47 2
        return $this->body->inOriginalFormat();
48
    }
49
50 21
    public function getAttribute($attributeName)
51
    {
52 21
        $messageProperties = $this->message->getProperties();
53 21
        if(array_key_exists($attributeName, $messageProperties))
54 21
        {
55 20
            return $messageProperties[$attributeName];
56
        }
57
58 2
        throw new \InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName));
59
    }
60
61 3
    public function __toString()
62
    {
63 3
        return json_encode(array(
64 3
            'routing_key' => $this->getRoutingKey(),
65 3
            'body' => (string) $this->body,
66 3
            'attributes' => $this->message->getProperties(),
67 3
        ));
68
    }
69
70 14
    private function getHeader($headerName)
71
    {
72 14
        $headers = $this->getHeaders();
73 14
        if(array_key_exists($headerName, $headers))
74 14
        {
75 12
            return $headers[$headerName];
76
        }
77
78 2
        return null;
79
    }
80
81 3
    public function getAttributes()
82
    {
83 3
        return $this->message->getProperties();
84
    }
85
86 11
    public function isLastRetry($retryOccurence = \Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_OCCURENCE)
87
    {
88 11
        $retryHeader = $this->getHeader(\Puzzle\AMQP\Consumers\Retry::DEFAULT_RETRY_HEADER);
89
90 11
        return ($retryHeader !== null && (int) $retryHeader >= $retryOccurence);
91
    }
92
93 3
    public function getRoutingKeyFromHeader()
94
    {
95 3
        return $this->getHeader('routing_key');
96
    }
97
98 2
    public function cloneIntoWritableMessage(WritableMessage $writable, $copyRoutingKey = false)
99
    {
100 2
        if($copyRoutingKey === true)
101 2
        {
102 1
            $writable->changeRoutingKey($this->getRoutingKey());
103 1
        }
104
105 2
        $writable->setBody($this->body);
106 2
        $writable->addHeaders($this->getHeaders());
0 ignored issues
show
Documentation introduced by
$this->getHeaders() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
108 2
        $attributes = $this->getAttributes();
109 2
        $skippedAttributes = array('timestamp', 'headers', 'app_id', 'routing_key');
110 2
        foreach($attributes as $attributeName => $value)
111
        {
112 2
            if(! in_array($attributeName, $skippedAttributes))
113 2
            {
114 2
                $writable->setAttribute($attributeName, $value);
115 2
            }
116 2
        }
117
118 2
        return $writable;
119
    }
120
}
121