Completed
Pull Request — master (#35)
by Nicolas
35:23
created

ReadableMessageModifier::changeBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Puzzle\AMQP\Workers;
4
5
use Puzzle\AMQP\ReadableMessage;
6
use Puzzle\AMQP\Messages\Body;
7
8
class ReadableMessageModifier
9
{
10
    private
11
        $originalMessage,
12
        $newRoutingKey,
13
        $newBody,
14
        $newAttributeValues,
15
        $newHeaders,
16
        $droppedHeaders;
17
    
18 5
    public function __construct(ReadableMessage $originalMessage)
19
    {
20 5
        $this->originalMessage = $originalMessage;
21
        
22 5
        $this->newRoutingKey= $originalMessage->getRoutingKey();
23 5
        $this->newBody = null;
24
        
25 5
        $this->newAttributeValues = [];
26 5
        $this->newHeaders = [];
27 5
        $this->droppedHeaders = [];
28 5
    }
29
    
30 1
    public function changeRoutingKey($newRoutingKey)
31
    {
32 1
        $this->newRoutingKey = $newRoutingKey;
33
        
34 1
        return $this;
35
    }
36
    
37 3
    public function changeBody(Body $newBody)
38
    {
39 3
        $this->newBody = $newBody;
40
        
41 3
        return $this;
42
    }
43
    
44 1
    public function changeAttribute($name, $value)
45
    {
46 1
        $this->newAttributeValues[$name] = $value;
47
        
48 1
        return $this;
49
    }
50
    
51 1
    public function addHeader($headerName, $value)
52
    {
53 1
        $this->newHeaders[$headerName] = $value;
54
        
55 1
        return $this;
56
    }
57 2
    public function dropHeader($headerName)
58
    {
59 2
        $this->droppedHeaders[] = $headerName;
60
        
61 2
        return $this;
62
    }
63
    
64 5
    public function build()
65
    {
66 5
        $properties = $this->buildAttributes(
67 5
            $this->originalMessage->getAttributes()
68 5
        );
69
        
70 5
        $properties['headers'] = $this->buildHeaders(
71 5
            $this->extractHeaders($properties)
72 5
        );
73
        
74 5
        return new MessageAdapter(
75 5
            new \Swarrot\Broker\Message($this->buildBody(), $properties)
76 5
        );
77
    }
78
    
79 5
    private function extractHeaders(array $properties)
80
    {
81 5
        $headers = [];
82
83 5
        if(isset($properties['headers']))
84 5
        {
85 5
            $headers = $properties['headers'];
86 5
        }
87
        
88 5
        return $headers;
89
    }
90
    
91 5
    private function buildBody()
92
    {
93 5
        if($this->newBody instanceof Body)
94 5
        {
95 3
            return $this->newBody->asTransported();
96
        }
97
        
98 2
        return $this->originalMessage->getBodyAsTransported();
99
    }
100
    
101 5 View Code Duplication
    private function buildAttributes(array $attributes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103 5
        if($this->newBody instanceof Body)
104 5
        {
105 3
            $attributes['content_type'] = $this->newBody->getContentType();
106 3
        }
107
        
108 5
        $attributes['routing_key'] = $this->newRoutingKey;
109
        
110 5
        foreach($this->newAttributeValues as $name => $value)
111
        {
112 1
            if(isset($attributes[$name]))
113 1
            {
114 1
                $attributes[$name] = $value;
115 1
            }
116 5
        }
117
        
118 5
        return $attributes;
119
    }
120
    
121 5 View Code Duplication
    private function buildHeaders(array $headers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123 5
        foreach($this->droppedHeaders as $droppedHeader)
124
        {
125 2
            if(isset($headers[$droppedHeader]))
126 2
            {
127 2
                unset($headers[$droppedHeader]);
128 2
            }
129 5
        }
130
        
131 5
        if($this->newBody instanceof Body)
132 5
        {
133 3
            $headers['transport_content_type'] = $this->newBody->getContentType();
134 3
        }
135
136 5
        foreach($this->newHeaders as $name => $value)
137
        {
138 1
            $headers[$name] = $value;
139 5
        }
140
        
141 5
        return $headers;
142
    }
143
}
144