Completed
Push — master ( 6be938...3b6271 )
by Edgar
02:01
created

AbstractNotification::setRetryCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace nstdio\notymo;
3
4
/**
5
 * Class AbstractNotification
6
 *
7
 * @package nstdio\notymo
8
 * @author  Edgar Asatryan <[email protected]>
9
 */
10
abstract class AbstractNotification implements PushNotificationInterface, LifeCycleCallback
11
{
12
    /**
13
     * @var MessageQueue
14
     */
15
    protected $messageQueue;
16
17
    /**
18
     * @var Connection
19
     */
20
    protected $stream;
21
22
    /**
23
     * @var CallbackInvoker
24
     */
25
    protected $invoker;
26
27
    /**
28
     * @var int
29
     */
30
    protected $retryCount = 3;
31
32
    /**
33
     * AbstractNotification constructor.
34
     */
35 20
    public function __construct()
36
    {
37 20
        $this->messageQueue = new MessageQueue();
38 20
        $this->invoker = new CallbackInvoker();
39 20
    }
40
41 8
    final public function send()
42
    {
43 8
        if ($this->messageQueue->isEmpty()) {
44 2
            return;
45
        }
46
47 6
        $this->openConnection();
48
49 6
        $attempt = 0;
50 6
        while ($attempt < $this->retryCount && !$this->messageQueue->isEmpty()) {
51
            /** @var MessageInterface $message */
52 6
            foreach ($this->messageQueue as $message) {
53 6
                $this->sendImpl($message);
54 6
            }
55 6
            $attempt++;
56 6
        }
57
58 6
        $this->notifyOnComplete($this->messageQueue);
59 6
        if ($this->messageQueue->isEmpty()) {
60 2
            $this->detach();
61 2
        }
62 6
        $this->stream->close();
63 6
    }
64
65
    abstract protected function sendImpl(MessageInterface $message);
66
67 6
    public function enqueue(MessageInterface $message)
68
    {
69 6
        $this->messageQueue->enqueue($message);
70 6
    }
71
72
    /**
73
     * @param Connection $wrapper
74
     */
75 9
    public function setStreamWrapper(Connection $wrapper)
76
    {
77 9
        $this->stream = $wrapper;
78 9
    }
79
80
    /**
81
     * @param int $count
82
     */
83 10
    public function setRetryCount($count)
84
    {
85 10
        $this->retryCount = intval($count);
86 10
        if ($this->retryCount <= 0) {
87 7
            throw new \InvalidArgumentException("retryCount must be grader then zero.");
88
        }
89 3
    }
90
91
    abstract protected function createPayload(MessageInterface $message);
92
93 6
    final protected function openConnection()
94
    {
95 6
        $this->lazyInitStream();
96
97 6
        $this->stream->open($this->getConnectionParams(), null);
98
99 6
        return $this;
100
    }
101
102 6
    protected function notifyOnComplete(MessageQueue $message)
103
    {
104 6
        $this->invoker->callOnComplete($message);
105 6
    }
106
107 5
    protected function notifyOnEachSent(MessageInterface $message, $feedBack)
108
    {
109 5
        $this->invoker->callOnEachSent($message, $feedBack);
110 5
    }
111
112 2
    public function onComplete(callable $listener)
113
    {
114 2
        $this->invoker->onComplete($listener);
115 2
    }
116
117 4
    public function onEachSent(callable $callback)
118
    {
119 4
        $this->invoker->onEachSent($callback);
120 4
    }
121
122 2
    public function detach()
123
    {
124 2
        $this->invoker->detach();
125 2
    }
126
127 6
    private function lazyInitStream()
128
    {
129 6
        if ($this->stream === null) {
130 1
            $this->stream = new CurlWrapper();
131 1
        }
132 6
    }
133
134
    /**
135
     * @return array
136
     */
137
    abstract protected function getConnectionParams();
138
}