Completed
Push — master ( a17349...e5d66a )
by Julián
05:42
created

AbstractNotification::getDefaultOptions()   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 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
    use OptionsTrait;
20
21
    const STATUS_PENDING = 0;
22
    const STATUS_SENT = 1;
23
24
    /**
25
     * @var \Jgut\Tify\Service\AbstractService
26
     */
27
    protected $service;
28
29
    /**
30
     * @var \Jgut\Tify\Message\AbstractMessage
31
     */
32
    protected $message;
33
34
    /**
35
     * @var \Jgut\Tify\Recipient\AbstractRecipient[]
36
     */
37
    protected $recipients = [];
38
39
    /**
40
     * @var int
41
     */
42
    protected $status = self::STATUS_PENDING;
43
44
    /**
45
     * Notification resultss.
46
     *
47
     * @var array
48
     */
49
    protected $results = [];
50
51
    /**
52
     * @param \Jgut\Tify\Service\AbstractService       $service
53
     * @param \Jgut\Tify\Message\AbstractMessage       $message
54
     * @param \Jgut\Tify\Recipient\AbstractRecipient[] $recipients
55
     * @param array                                    $options
56
     */
57
    public function __construct(
58
        AbstractService $service,
59
        AbstractMessage $message,
60
        array $recipients = [],
61
        array $options = []
62
    ) {
63
        $this->service = $service;
64
        $this->message = $message;
65
66
        foreach ($recipients as $recipient) {
67
            $this->addRecipient($recipient);
68
        }
69
70
        $this->setOptions(array_merge($this->getDefaultOptions(), $options));
71
    }
72
73
    /**
74
     * Get default notification options.
75
     *
76
     * @return array
77
     */
78
    protected function getDefaultOptions()
79
    {
80
        return [];
81
    }
82
83
    /**
84
     * Get service.
85
     *
86
     * @return \Jgut\Tify\Service\AbstractService
87
     */
88
    final public function getService()
89
    {
90
        return $this->service;
91
    }
92
93
    /**
94
     * Set service.
95
     *
96
     * @param \Jgut\Tify\Service\AbstractService $service
97
     */
98
    abstract public function setService(AbstractService $service);
99
100
    /**
101
     * Get message.
102
     *
103
     * @return \Jgut\Tify\Message\AbstractMessage
104
     */
105
    final public function getMessage()
106
    {
107
        return $this->message;
108
    }
109
110
    /**
111
     * Set message.
112
     *
113
     * @param \Jgut\Tify\Message\AbstractMessage $message
114
     */
115
    abstract public function setMessage(AbstractMessage $message);
116
117
    /**
118
     * Retrieve list of recipients.
119
     *
120
     * @return \Jgut\Tify\Recipient\AbstractRecipient[]
121
     */
122
    final public function getRecipients()
123
    {
124
        return array_values($this->recipients);
125
    }
126
127
    /**
128
     * Add recipient.
129
     *
130
     * @param \Jgut\Tify\Recipient\AbstractRecipient $recipient
131
     */
132
    abstract public function addRecipient(AbstractRecipient $recipient);
133
134
    /**
135
     * Retrieve notification status.
136
     *
137
     * @return int
138
     */
139
    final public function getStatus()
140
    {
141
        return $this->status;
142
    }
143
144
    /**
145
     * Check if notification status is pushed.
146
     *
147
     * @return bool
148
     */
149
    final public function isSent()
150
    {
151
        return $this->status === static::STATUS_SENT;
152
    }
153
154
    /**
155
     * Set notification as sent.
156
     *
157
     * @param array $results
158
     */
159
    final public function setSent(array $results = [])
160
    {
161
        $this->status = static::STATUS_SENT;
162
        $this->results = $results;
163
    }
164
165
    /**
166
     * Set notification pending (not sent).
167
     */
168
    final public function setPending()
169
    {
170
        $this->status = static::STATUS_PENDING;
171
        $this->results = [];
172
    }
173
174
    /**
175
     * Retrieve results.
176
     *
177
     * @return \Jgut\Tify\Result[]
178
     */
179
    final public function getResults()
180
    {
181
        return $this->results;
182
    }
183
184
    /**
185
     * Retrieve recipients tokens list.
186
     *
187
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be \Generator?

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...
188
     */
189
    final public function getTokens()
190
    {
191
        foreach ($this->recipients as $recipient) {
192
            var_dump($recipient->getToken());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($recipient->getToken()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
193
            yield $recipient->getToken();
194
        }
195
    }
196
}
197