Completed
Push — master ( 613542...020c5c )
by Julián
03:39
created

AbstractNotification::isPending()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify\Notification;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Jgut\Tify\Recipient\AbstractRecipient;
14
use Jgut\Tify\Message\AbstractMessage;
15
use Jgut\Tify\OptionsTrait;
16
use Jgut\Tify\Service\AbstractService;
17
18
/**
19
 * Class AbstractNotification
20
 */
21
abstract class AbstractNotification
22
{
23
    use OptionsTrait;
24
25
    const STATUS_PENDING = 0;
26
    const STATUS_SENT = 1;
27
28
    /**
29
     * @var \Jgut\Tify\Service\AbstractService
30
     */
31
    protected $service;
32
33
    /**
34
     * @var \Jgut\Tify\Message\AbstractMessage
35
     */
36
    protected $message;
37
38
    /**
39
     * @var \Jgut\Tify\Recipient\AbstractRecipient[]
40
     */
41
    protected $recipients = [];
42
43
    /**
44
     * @var int
45
     */
46
    protected $status = self::STATUS_PENDING;
47
48
    /**
49
     * Notification resultss.
50
     *
51
     * @var array
52
     */
53
    protected $results = [];
54
55
    /**
56
     * @param \Jgut\Tify\Service\AbstractService       $service
57
     * @param \Jgut\Tify\Message\AbstractMessage       $message
58
     * @param \Jgut\Tify\Recipient\AbstractRecipient[] $recipients
59
     * @param array                                    $options
60
     */
61
    public function __construct(
62
        AbstractService $service,
63
        AbstractMessage $message,
64
        array $recipients = [],
65
        array $options = []
66
    ) {
67
        $this->service = $service;
68
        $this->message = $message;
69
70
        foreach ($recipients as $recipient) {
71
            $this->addRecipient($recipient);
72
        }
73
74
        $this->options = new ArrayCollection(array_merge($this->getDefaultOptions(), $options));
75
    }
76
77
    /**
78
     * Get default notification options.
79
     *
80
     * @return array
81
     */
82
    abstract protected function getDefaultOptions();
83
84
    /**
85
     * Get service.
86
     *
87
     * @return \Jgut\Tify\Service\AbstractService
88
     */
89
    final public function getService()
90
    {
91
        return $this->service;
92
    }
93
94
    /**
95
     * Set service.
96
     *
97
     * @param \Jgut\Tify\Service\AbstractService $service
98
     */
99
    abstract public function setService(AbstractService $service);
100
101
    /**
102
     * Get message.
103
     *
104
     * @return \Jgut\Tify\Message\AbstractMessage
105
     */
106
    final public function getMessage()
107
    {
108
        return $this->message;
109
    }
110
111
    /**
112
     * Set message.
113
     *
114
     * @param \Jgut\Tify\Message\AbstractMessage $message
115
     */
116
    abstract public function setMessage(AbstractMessage $message);
117
118
    /**
119
     * Retrieve list of recipients.
120
     *
121
     * @return \Jgut\Tify\Recipient\AbstractRecipient[]
122
     */
123
    final public function getRecipients()
124
    {
125
        return array_values($this->recipients);
126
    }
127
128
    /**
129
     * Add recipient.
130
     *
131
     * @param \Jgut\Tify\Recipient\AbstractRecipient $recipient
132
     */
133
    abstract public function addRecipient(AbstractRecipient $recipient);
134
135
    /**
136
     * Retrieve notification status.
137
     *
138
     * @return int
139
     */
140
    final public function getStatus()
141
    {
142
        return $this->status;
143
    }
144
145
    /**
146
     * Check if notification status is sent.
147
     *
148
     * @return bool
149
     */
150
    final public function isSent()
151
    {
152
        return $this->status === static::STATUS_SENT;
153
    }
154
155
    /**
156
     * Check if notification status is pending.
157
     *
158
     * @return bool
159
     */
160
    final public function isPending()
161
    {
162
        return $this->status === static::STATUS_PENDING;
163
    }
164
165
    /**
166
     * Set notification status.
167
     *
168
     * @param int                 $status
169
     * @param \Jgut\Tify\Result[] $results
170
     *
171
     * @throws \InvalidArgumentException
172
     *
173
     * @return $this
174
     */
175
    final public function setStatus($status, array $results = [])
176
    {
177
        if (!in_array($status, [self::STATUS_PENDING, self::STATUS_SENT])) {
178
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid status', $status));
179
        }
180
181
        $this->status = $status;
182
183
        $this->results = [];
184
        if ($status === static::STATUS_SENT) {
185
            $this->results = $results;
186
        }
187
188
        return $this;
189
    }
190
191
    /**
192
     * Retrieve results.
193
     *
194
     * @return \Jgut\Tify\Result[]
195
     */
196
    final public function getResults()
197
    {
198
        return $this->results;
199
    }
200
201
    /**
202
     * Retrieve recipients tokens list.
203
     *
204
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
205
     */
206
    final public function getTokens()
207
    {
208
        return array_keys($this->recipients);
209
    }
210
}
211