Completed
Push — master ( ec33f6...e51686 )
by Julián
02:46
created

AbstractNotification::setSent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
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 Jgut\Tify\Recipient\AbstractRecipient;
13
use Jgut\Tify\Message\AbstractMessage;
14
use Jgut\Tify\OptionsTrait;
15
use Jgut\Tify\Service\AbstractService;
16
17
abstract class AbstractNotification
18
{
19
    const STATUS_PENDING = 0;
20
    const STATUS_SENT = 1;
21
22
    use OptionsTrait;
23
24
    /**
25
     * Default notification options.
26
     *
27
     * @var array
28
     */
29
    protected $defaultOptions = [];
30
31
    /**
32
     * @var \Jgut\Tify\Service\AbstractService
33
     */
34
    protected $service;
35
36
    /**
37
     * @var \Jgut\Tify\Message\AbstractMessage
38
     */
39
    protected $message;
40
41
    /**
42
     * @var \Jgut\Tify\Recipient\AbstractRecipient[]
43
     */
44
    protected $recipients = [];
45
46
    /**
47
     * @var int
48
     */
49
    protected $status = self::STATUS_PENDING;
50
51
    /**
52
     * @var \DateTime
53
     */
54
    protected $pushTime;
55
56
    /**
57
     * Notification resultss.
58
     *
59
     * @var array
60
     */
61
    protected $results = [];
62
63
    /**
64
     * @param \Jgut\Tify\Service\AbstractService       $service
65
     * @param \Jgut\Tify\Message\AbstractMessage       $message
66
     * @param \Jgut\Tify\Recipient\AbstractRecipient[] $recipients
67
     * @param array                                    $options
68
     */
69
    public function __construct(
70
        AbstractService $service,
71
        AbstractMessage $message,
72
        array $recipients = [],
73
        array $options = []
74
    ) {
75
        $this->service = $service;
76
        $this->message = $message;
77
78
        foreach ($recipients as $recipient) {
79
            $this->addRecipient($recipient);
80
        }
81
82
        $this->setOptions(array_merge($this->defaultOptions, $options));
83
    }
84
85
    /**
86
     * Get service.
87
     *
88
     * @return \Jgut\Tify\Service\AbstractService
89
     */
90
    final public function getService()
91
    {
92
        return $this->service;
93
    }
94
95
    /**
96
     * Set service.
97
     *
98
     * @param \Jgut\Tify\Service\AbstractService $service
99
     */
100
    abstract public function setService(AbstractService $service);
101
102
    /**
103
     * Get message.
104
     *
105
     * @return \Jgut\Tify\Message\AbstractMessage
106
     */
107
    final public function getMessage()
108
    {
109
        return $this->message;
110
    }
111
112
    /**
113
     * Set message.
114
     *
115
     * @param \Jgut\Tify\Message\AbstractMessage $message
116
     */
117
    abstract public function setMessage(AbstractMessage $message);
118
119
    /**
120
     * Retrieve list of recipients.
121
     *
122
     * @return \Jgut\Tify\Recipient\AbstractRecipient[]
123
     */
124
    final public function getRecipients()
125
    {
126
        return $this->recipients;
127
    }
128
129
    /**
130
     * Add recipient.
131
     *
132
     * @param \Jgut\Tify\Recipient\AbstractRecipient $recipient
133
     */
134
    abstract public function addRecipient(AbstractRecipient $recipient);
135
136
    /**
137
     * Retrieve notification status.
138
     *
139
     * @return int
140
     */
141
    final public function getStatus()
142
    {
143
        return $this->status;
144
    }
145
146
    /**
147
     * Check if notification status is pushed.
148
     *
149
     * @return bool
150
     */
151
    final public function isSent()
152
    {
153
        return $this->status === static::STATUS_SENT;
154
    }
155
156
    /**
157
     * Set notification as sent.
158
     *
159
     * @param array $results
160
     */
161
    final public function setSent(array $results = [])
162
    {
163
        $this->status = static::STATUS_SENT;
164
        $this->pushTime = new \DateTime;
165
        $this->results = $results;
166
    }
167
168
    /**
169
     * Set notification pending (not sent).
170
     */
171
    final public function setPending()
172
    {
173
        $this->status = static::STATUS_PENDING;
174
        $this->pushTime = null;
175
        $this->results = [];
176
    }
177
178
    /**
179
     * Retrieve sent time.
180
     *
181
     * @return \DateTime
182
     */
183
    final public function getPushTime()
184
    {
185
        return $this->pushTime;
186
    }
187
188
    /**
189
     * Retrieve results.
190
     *
191
     * @return array
192
     */
193
    final public function getResults()
194
    {
195
        return $this->results;
196
    }
197
198
    /**
199
     * Retrieve recipients tokens list.
200
     *
201
     * @return array
202
     */
203
    final public function getTokens()
204
    {
205
        $tokens = [];
206
207
        foreach ($this->recipients as $recipient) {
208
            $tokens[] = $recipient->getToken();
209
        }
210
211
        return array_unique(array_filter($tokens));
212
    }
213
}
214