SimpleMessagingGateway::setReplyChannel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 4 Features 0
Metric Value
c 4
b 4
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace PEIP\Gateway;
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
/*
14
 * SimpleMessagingGateway
15
 * Basic implementation of a messaging gateway
16
 *
17
 * @author Timo Michna <timomichna/yahoo.de>
18
 * @package PEIP
19
 * @subpackage gateway
20
 * @implements \PEIP\INF\Gateway\MessagingGateway, \PEIP\INF\Message\MessageBuilder
21
 */
22
23
use PEIP\Message\MessageBuilder;
24
25
class SimpleMessagingGateway implements
26
        \PEIP\INF\Gateway\MessagingGateway,
27
        \PEIP\INF\Message\MessageBuilder
28
{
29
    protected $requestChannel,
30
        $replyChannel,
31
        $messageClass = '\PEIP\Message\GenericMessage',
32
        $defaultHeaders,
33
        $messageBuilder;
34
35
    /**
36
     * constructor.
37
     *
38
     * @param \PEIP\INF\Channel\Channel         $requestChannel The default channel to send requests from the gateway
39
     * @param \PEIP\INF\Channel\PollableChannel $replyChannel   The default channel to receive requests from the gateway
40
     * @param array                             $defaultHeaders The default headers to apply to request messages
41
     */
42
    public function __construct(\PEIP\INF\Channel\Channel $requestChannel, \PEIP\INF\Channel\Channel $replyChannel = null, array $defaultHeaders = [])
43
    {
44
        $this->setRequestChannel($requestChannel);
45
        if ($replyChannel) {
46
            $this->setReplyChannel($replyChannel);
47
        }
48
        $this->defaultHeaders = $defaultHeaders;
49
        $this->messageBuilder = MessageBuilder::getInstance($this->messageClass);
50
    }
51
52
    /**
53
     * sets the channel to send requests from the gateway.
54
     *
55
     * @return
56
     */
57
    public function setRequestChannel(\PEIP\INF\Channel\Channel $requestChannel)
58
    {
59
        $this->requestChannel = $requestChannel;
60
    }
61
62
    /**
63
     * sets the default channel to receive requests from the gateway.
64
     *
65
     * @param \PEIP\INF\Channel\PollableChannel $replyChannel The default channel to receive requests from the gateway
66
     *
67
     * @return
68
     */
69
    public function setReplyChannel(\PEIP\INF\Channel\Channel $replyChannel)
70
    {
71
        if (!($replyChannel instanceof \PEIP\INF\Channel\PollableChannel)) {
72
            throw new \InvalidArgumentException('reply channel must be instance of \PEIP\INF\Channel\PollableChannel.');
73
        }
74
        $this->replyChannel = $replyChannel;
75
    }
76
77
    /**
78
     * sends a request/message through the gateway.
79
     *
80
     * @param mixed $content the content/payload for the message to send
81
     *
82
     * @return
83
     */
84
    public function send($content)
85
    {
86
        return $this->requestChannel->send($this->buildMessage($content));
87
    }
88
89
    /**
90
     * receives a request/message from the gateway.
91
     *
92
     * @return mixed content/payload of the received message
93
     */
94
    public function receive()
95
    {
96
        if (!isset($this->replyChannel)) {
97
            throw new \LogicException('No replyChannel set.');
98
        }
99
        $message = $this->replyChannel->receive();
100
        if ($message) {
101
            return $message->getContent();
102
        }
103
    }
104
105
    /**
106
     * sends and receives a request/message through the gateway.
107
     *
108
     * @param \Order $content the content/payload for the message to send
109
     *
110
     * @return mixed content/payload of the received message
111
     */
112
    public function sendAndReceive($content)
113
    {
114
        $this->send($content);
115
        try {
116
            $res = $this->receive();
117
        } catch (\Exception $e) {
118
            return;
119
        }
120
121
        return $res;
122
    }
123
124
    /**
125
     * builds the message to send from given content/payload.
126
     *
127
     * @param mixed $content the content/payload for the message to send
128
     *
129
     * @return \PEIP\INF\Message\Message the built message
130
     */
131
    protected function buildMessage($content)
132
    {
133
        return $this->getMessageBuilder()->setContent($content)->build();
134
    }
135
136
    /**
137
     * returns the message builder instance for the registerd message class.
138
     *
139
     * @return MessageBuilder message builder instance for the registerd message class
140
     */
141
    protected function getMessageBuilder()
142
    {
143
        return isset($this->messageBuilder) && ($this->messageBuilder->getMessageClass() == $this->getMessageClass())
144
            ? $this->messageBuilder
145
            : $this->messageBuilder = MessageBuilder::getInstance($this->messageClass)->setHeaders($this->defaultHeaders);
146
    }
147
148
    /**
149
     * registers the message class to create instances from by the gateway.
150
     *
151
     * @param string $messageClass message class to create instances from
152
     *
153
     * @return
154
     */
155
    public function setMessageClass($messageClass)
156
    {
157
        $this->messageClass = $messageClass;
158
    }
159
160
    /**
161
     * returns the message class to create instances from.
162
     *
163
     * @return string message class to create instances from
164
     */
165
    public function getMessageClass()
166
    {
167
        return $this->messageClass;
168
    }
169
}
170