Completed
Push — master ( 451433...4e2c90 )
by Dmitry
05:05
created

ActiveMQSendReciveAckCheck::getTimeOut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace TonicHealthCheck\Check\ActiveMQ\SendReciveAck;
4
5
use Stomp\StatefulStomp as StatefulStomp;
6
use Stomp\Transport\Message;
7
use Stomp\Exception\StompException;
8
use TonicHealthCheck\Check\ActiveMQ\AbstractActiveMQCheck;
9
use TonicHealthCheck\Check\ActiveMQ\SendReciveAck\Exception\ActiveMQReceiveAckCheckException;
10
use TonicHealthCheck\Check\ActiveMQ\SendReciveAck\Exception\ActiveMQSendAckCheckException;
11
use TonicHealthCheck\Check\ActiveMQ\SendReciveAck\Exception\ActiveMQSendReciveAckCheckException;
12
13
/**
14
 * Class ActiveMQSendReciveAckCheck.
15
 */
16
class ActiveMQSendReciveAckCheck extends AbstractActiveMQCheck
17
{
18
    const CHECK = 'activemq-send-recive-ack-performCheck';
19
20
    const TEST_DESTINATION = '/queue/test';
21
    const TEST_BODY = 'The story is about a plain conjurer and a starship captain. It takes place in a galaxy-spanning theocracy.';
22
    const TEST_TIME_OUT = 10;
23
24
    /**
25
     * @var StatefulStomp
26
     */
27
    private $statefulStomp;
28
29
    /**
30
     * @var string
31
     */
32
    private $destination = self::TEST_DESTINATION;
33
34
    /**
35
     * @var string
36
     */
37
    private $body = self::TEST_BODY;
38
39
    /**
40
     * @var int
41
     */
42
    private $timeOut = self::TEST_TIME_OUT;
43
44
    /**
45
     * ActiveMQSendReciveAckCheck constructor.
46
     *
47
     * @param null          $checkNode
48
     * @param StatefulStomp $statefulStomp
49
     * @param string        $destination
50
     * @param string        $body
51
     * @param int           $timeOut
52
     */
53 5
    public function __construct(
54
        $checkNode,
55
        StatefulStomp $statefulStomp,
56
        $destination = null,
57
        $body = null,
58
        $timeOut = null
59
    ) {
60 5
        parent::__construct($checkNode);
61 5
        $this->setStatefulStomp($statefulStomp);
62
63 5
        if (null !== $destination) {
64 5
            $this->setDestination($destination);
65
        }
66
67 5
        if (null !== $body) {
68 5
            $this->setBody($body);
69
        }
70
71 5
        if (null !== $timeOut) {
72 5
            $this->setTimeOut($timeOut);
73
        }
74 5
    }
75
76
    /**
77
     * test activeMQ send&recive&acknowledgment.
78
     *
79
     * @throws ActiveMQReceiveAckCheckException
80
     * @throws ActiveMQSendAckCheckException
81
     * @throws ActiveMQSendReciveAckCheckException
82
     */
83 5
    public function performCheck()
84
    {
85
        try {
86 5
            $message = new Message($this->getBody());
87
            try {
88 5
                $this->getStatefulStomp()->send($this->getDestination(), $message);
89 1
            } catch (StompException $e) {
90 1
                throw ActiveMQSendAckCheckException::canNotSent($this->getDestination(), $this->getBody(), $e);
91
            }
92 4
            if (count($this->getStatefulStomp()->getSubscriptions()) > 0) {
93 1
                $this->getStatefulStomp()->unsubscribe();
94
            }
95 4
            $this->getStatefulStomp()->subscribe($this->getDestination(), null, 'client-individual');
96 3
            $this->getStatefulStomp()->getClient()->getConnection()->setReadTimeout($this->getTimeOut() / 2);
97 3
            $this->getStatefulStomp()->getClient()->setReceiptWait($this->getTimeOut());
98
99
            try {
100 3
                $message = $this->getStatefulStomp()->read();
101 1
            } catch (StompException $e) {
102 1
                throw ActiveMQReceiveAckCheckException::canNotReceive($this->getDestination(), $e);
103
            }
104
105 2
            if ($message !== false) {
106 1
                $this->getStatefulStomp()->ack($message);
107
            } else {
108 1
                throw ActiveMQReceiveAckCheckException::canNotReceive($this->getDestination(), new \Exception('Message didn\'t receive'));
109
            }
110
111 1
            $this->getStatefulStomp()->unsubscribe();
112 4
        } catch (StompException $e) {
113 1
            throw ActiveMQSendReciveAckCheckException::internalProblem($e);
114
        }
115 1
    }
116
117
    /**
118
     * @return StatefulStomp
119
     */
120 5
    public function getStatefulStomp()
121
    {
122 5
        return $this->statefulStomp;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 5
    public function getDestination()
129
    {
130 5
        return $this->destination;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 5
    public function getBody()
137
    {
138 5
        return $this->body;
139
    }
140
141
    /**
142
     * @return int
143
     */
144 3
    public function getTimeOut()
145
    {
146 3
        return $this->timeOut;
147
    }
148
149
    /**
150
     * @param StatefulStomp $statefulStomp
151
     */
152 5
    protected function setStatefulStomp(StatefulStomp $statefulStomp)
153
    {
154 5
        $this->statefulStomp = $statefulStomp;
155 5
    }
156
157
    /**
158
     * @param string $destination
159
     */
160 5
    protected function setDestination($destination)
161
    {
162 5
        $this->destination = $destination;
163 5
    }
164
165
    /**
166
     * @param string $body
167
     */
168 5
    protected function setBody($body)
169
    {
170 5
        $this->body = $body;
171 5
    }
172
173
    /**
174
     * @param int $timeOut
175
     */
176 5
    protected function setTimeOut($timeOut)
177
    {
178 5
        $this->timeOut = $timeOut;
179 5
    }
180
}
181