Test Failed
Pull Request — master (#29)
by Nicolas
02:57
created

ReadableMessageModifier::addHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
    public function __construct(ReadableMessage $originalMessage)
19
    {
20
        $this->originalMessage = $originalMessage;
21
        
22
        $this->newRoutingKey= $originalMessage->getRoutingKey();
23
        $this->newBody = null;
24
        
25
        $this->newAttributeValues = [];
26
        $this->newHeaders = [];
27
        $this->droppedHeaders = [];
28
    }
29
    
30
    public function changeRoutingKey($newRoutingKey)
31
    {
32
        $this->newRoutingKey = $newRoutingKey;
33
        
34
        return $this;
35
    }
36
    
37
    public function changeBody(Body $newBody)
38
    {
39
        $this->newBody = $newBody;
40
        
41
        return $this;
42
    }
43
    
44
    public function changeAttribute($name, $value)
45
    {
46
        $this->newAttributeValues[$name] = $value;
47
        
48
        return $this;
49
    }
50
    
51
    public function addHeader($headerName, $value)
52
    {
53
        $this->newHeaders[$headerName] = $value;
54
        
55
        return $this;
56
    }
57
    public function dropHeader($headerName)
58
    {
59
        $this->droppedHeaders[] = $headerName;
60
        
61
        return $this;
62
    }
63
    
64
    public function build()
65
    {
66
        $properties = $this->buildAttributes(
67
            $this->originalMessage->getAttributes()
68
        );
69
        
70
        $properties['headers'] = $this->buildHeaders(
71
            $this->extractHeaders($properties)
72
        );
73
        
74
        return new MessageAdapter(
75
            new \Swarrot\Broker\Message($this->buildBody(), $properties)
76
        );
77
    }
78
    
79
    private function extractHeaders(array $properties)
80
    {
81
        $headers = [];
82
83
        if(isset($properties['headers']))
84
        {
85
            $headers = $properties['headers'];
86
        }
87
        
88
        return $headers;
89
    }
90
    
91
    private function buildBody()
92
    {
93
        if($this->newBody instanceof Body)
94
        {
95
            return $this->newBody->asTransported();
96
        }
97
        
98
        return $this->originalMessage->getBodyAsTransported();
99
    }
100
    
101
    private function buildAttributes(array $attributes)
102
    {
103
        if($this->newBody instanceof Body)
104
        {
105
            $attributes['content_type'] = $this->newBody->getContentType();
106
        }
107
        
108
        $attributes['routing_key'] = $this->newRoutingKey;
109
        
110
        foreach($this->newAttributeValues as $name => $value)
111
        {
112
            if(isset($attributes[$name]))
113
            {
114
                $attributes[$name] = $value;
115
            }
116
        }
117
        
118
        return $attributes;
119
    }
120
    
121
    private function buildHeaders(array $headers)
122
    {
123
        foreach($this->droppedHeaders as $droppedHeader)
124
        {
125
            if(isset($headers[$droppedHeader]))
126
            {
127
                unset($headers[$droppedHeader]);
128
            }
129
        }
130
        
131
        foreach($this->newHeaders as $name => $value)
132
        {
133
            $headers[$name] = $value;
134
        }
135
        
136
        return $headers;
137
    }
138
}
139