AbstractNotification   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 0
loc 139
ccs 56
cts 62
cp 0.9032
rs 10
c 0
b 0
f 0

17 Methods

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