MessageBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace PEIP\Message;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
/*
13
 * MessageBuilder
14
 * Util class to easily create and copy messages
15
 *
16
 * @author Timo Michna <timomichna/yahoo.de>
17
 * @package PEIP
18
 * @subpackage message
19
 * @implements \PEIP\INF\Message\MessageBuilder, \PEIP\INF\Factory\DedicatedFactory
20
 */
21
22
use PEIP\Base\GenericBuilder;
23
use PEIP\Factory\DedicatedFactory;
24
25
class MessageBuilder implements
26
        \PEIP\INF\Message\MessageBuilder,
27
        \PEIP\INF\Factory\DedicatedFactory
28
{
29
    protected $messageClass,
30
        $factory,
31
        $headers = [],
32
        $payload;
33
34
    /**
35
     * constructor.
36
     *
37
     * @param string $messageClass the message-class to build instances for
38
     */
39
    public function __construct($messageClass = '\PEIP\Message\GenericMessage')
40
    {
41
        $this->messageClass = $messageClass;
42
        $this->factory = DedicatedFactory::getfromCallable([$messageClass, 'build']);
43
    }
44
45
    /**
46
     * copies headers to message to build.
47
     *
48
     * @param array $headers the headers to set
49
     *
50
     * @return MessageBuilder $this
51
     */
52
    public function copyHeaders(array $headers)
53
    {
54
        $this->headers = array_merge($this->headers, $headers);
55
56
        return $this;
57
    }
58
59
    /**
60
     * copies headers to message to build if not set allerady.
61
     *
62
     * @param array $headers the headers to set
63
     *
64
     * @return MessageBuilder $this
65
     */
66
    public function copyHeadersIfAbsent(array $headers)
67
    {
68
        $this->headers = array_merge($headers, $this->headers);
69
70
        return $this;
71
    }
72
73
    /**
74
     * removes a header from message to build.
75
     *
76
     * @param string $headerName the name of the header
77
     *
78
     * @return MessageBuilder $this
79
     */
80
    public function removeHeader($headerName)
81
    {
82
        unset($this->headers[$headerName]);
83
84
        return $this;
85
    }
86
87
    /**
88
     * sets a header for the message to build.
89
     *
90
     * @param string $headerName  the name of the header
91
     * @param mixed  $headerValue the value for the header
92
     *
93
     * @return MessageBuilder $this
94
     */
95
    public function setHeader($headerName, $headerValue)
96
    {
97
        $this->headers[$headerName] = $headerValue;
98
99
        return $this;
100
    }
101
102
    /**
103
     * sets all headers for the message to build.
104
     *
105
     * @param array $headers the headers to set
106
     *
107
     * @return MessageBuilder $this
108
     */
109
    public function setHeaders(array $headers)
110
    {
111
        $this->headers = $headers;
112
113
        return $this;
114
    }
115
116
    /**
117
     * returns the headers for the message to build.
118
     *
119
     * @return array the headers for the message to build
120
     */
121
    public function getHeaders()
122
    {
123
        return $this->headers;
124
    }
125
126
    /**
127
     * creates a new message instance with given headers.
128
     *
129
     * @param $arguments
130
     *
131
     * @return
132
     */
133
    public function build(array $headers = [])
134
    {
135
        $this->copyHeaders($headers);
136
137
        return GenericBuilder::getInstance($this->messageClass)
138
            ->build([$this->payload, new \ArrayObject($this->headers)]);
139
    }
140
141
    /**
142
     * sets the content/payload for the message to build.
143
     *
144
     * @param mixed $payload payload for the message to build
145
     *
146
     * @return MessageBuilder $this
147
     */
148
    public function setContent($payload)
149
    {
150
        $this->payload = $payload;
151
152
        return $this;
153
    }
154
155
    /**
156
     * returns a instance of MessageBuilder for given message-class.
157
     *
158
     * @static
159
     *
160
     * @param string $messageClass the message class to build from the builder
161
     *
162
     * @return MessageBuilder new instance of MessageBuilder
163
     */
164
    public static function getInstance($messageClass = '\PEIP\Message\GenericMessage')
165
    {
166
        return new  self($messageClass);
167
    }
168
169
    /**
170
     * returns a instance of MessageBuilder for message-class derived from given message.
171
     *
172
     * @static
173
     *
174
     * @param \PEIP\INF\Message\Message $message the message to get class to build from the builder
175
     *
176
     * @return MessageBuilder new instance of MessageBuilder
177
     */
178
    public static function getInstanceFromMessage(\PEIP\INF\Message\Message $message)
179
    {
180
        return new self(get_class($message));
181
    }
182
183
    /**
184
     * sets the message-class to build new instances for.
185
     *
186
     * @param string $messageClass the message-class to build new instances for
187
     */
188
    public function setMessageClass($messageClass)
189
    {
190
        $this->messageClass = $messageClass;
191
    }
192
193
    /**
194
     * returns the message-class to build new instances for.
195
     *
196
     * @return string the message-class to build new instances for
197
     */
198
    public function getMessageClass()
199
    {
200
        return $this->messageClass;
201
    }
202
}
203