Completed
Push — master ( f47fb6...c9af04 )
by Julián
02:24
created

Message::getMappedParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
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 8
rs 9.4285
cc 2
eloc 4
nc 2
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;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
14
/**
15
 * Push message.
16
 */
17
class Message
18
{
19
    use ParameterTrait {
20
        ParameterTrait::hasParameter as hasDefinedParameter;
21
        ParameterTrait::getParameter as getDefinedParameter;
22
        ParameterTrait::setParameter as setDefinedParameter;
23
    }
24
25
    /**
26
     * Valid message parameters.
27
     *
28
     * @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG
29
     *          /Chapters/TheNotificationPayload.html
30
     * @see https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json
31
     *
32
     * @var array
33
     */
34
    static protected $validParameters = [
35
        // Common
36
        'title',
37
        'body',
38
39
        // Common mapped
40
        'title_loc_key',
41
        'title_loc_args',
42
        'body_loc_key',
43
        'body_loc_args',
44
45
        // APNS
46
        'action-loc-key',
47
        'launch-image',
48
49
        // GCM
50
        'icon',
51
        'sound',
52
        'tag',
53
        'color',
54
        'click_action',
55
    ];
56
57
    /**
58
     * Parameter key map
59
     *
60
     * @var array
61
     */
62
    static protected $parameterMap = [
63
        'title-loc-key'  => 'title_loc_key',
64
        'title-loc-args' => 'title_loc_args',
65
        'loc-key'        => 'body_loc_key',
66
        'loc-args'       => 'body_loc_args',
67
    ];
68
69
    /**
70
     * Reserved payload keys.
71
     *
72
     * @var array
73
     */
74
    protected static $reservedPayloadRegex = [
75
        // APNS
76
        '/^apc$/',
77
78
        // GCM
79
        '/^(google|gcm)/',
80
        '/^from$/',
81
        '/^collapse_key$/',
82
        '/^delay_while_idle$/',
83
        '/^time_to_live$/',
84
        '/^restricted_package_name$/',
85
        '/^dry_run$/',
86
        '/^priority$/',
87
        '/^content_available$/',
88
    ];
89
90
    /**
91
     * Payload prefix
92
     *
93
     * @var string
94
     */
95
    protected $payloadPrefix = 'data_';
96
97
    /**
98
     * Message payload.
99
     *
100
     * @var \Doctrine\Common\Collections\ArrayCollection
101
     */
102
    protected $payload;
103
104
    /**
105
     * Constructor.
106
     *
107
     * @param array $parameters
108
     */
109
    public function __construct(array $parameters = [])
110
    {
111
        $this->setParameters($parameters);
112
113
        $this->payload = new ArrayCollection;
114
    }
115
116
    /**
117
     * Convenience method to set message title.
118
     *
119
     * @param string $title
120
     *
121
     * @return $this
122
     */
123
    public function setTitle($title)
124
    {
125
        $this->setParameter('title', $title);
126
127
        return $this;
128
    }
129
130
    /**
131
     * Convenience method to set message body.
132
     *
133
     * @param string $body
134
     *
135
     * @return $this
136
     */
137
    public function setBody($body)
138
    {
139
        $this->setParameter('body', $body);
140
141
        return $this;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function hasParameter($parameter)
148
    {
149
        return $this->hasDefinedParameter($this->getMappedParameter($parameter));
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function getParameter($parameter, $default = null)
156
    {
157
        return $this->getDefinedParameter($this->getMappedParameter($parameter), $default);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     *
163
     * @throws \InvalidArgumentException
164
     */
165
    public function setParameter($parameter, $value)
166
    {
167
        $parameter = $this->getMappedParameter($parameter);
168
169
        if (!in_array($parameter, static::$validParameters)) {
170
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid message parameter', $parameter));
171
        }
172
173
        return $this->setDefinedParameter($parameter, $value);
174
    }
175
176
    private function getMappedParameter($parameter)
177
    {
178
        if (array_key_exists($parameter, static::$parameterMap)) {
179
            return static::$parameterMap[$parameter];
180
        }
181
182
        return $parameter;
183
    }
184
185
    /**
186
     * Retrieve payload prefix.
187
     *
188
     * @return string
189
     */
190
    public function getPayloadPrefix()
191
    {
192
        return $this->payloadPrefix;
193
    }
194
195
    /**
196
     * Set payload prefix.
197
     *
198
     * @param string $prefix
199
     *
200
     * @return $this
201
     */
202
    public function setPayloadPrefix($prefix)
203
    {
204
        $this->payloadPrefix = trim($prefix);
205
206
        return $this;
207
    }
208
209
    /**
210
     * Get payload data.
211
     *
212
     * @return array
213
     */
214
    public function getPayloadData()
215
    {
216
        return $this->payload->toArray();
217
    }
218
219
    /**
220
     * Set payload data.
221
     *
222
     * @param array $data
223
     *
224
     * @throws \InvalidArgumentException
225
     *
226
     * @return $this
227
     */
228
    public function setPayloadData(array $data)
229
    {
230
        $this->payload->clear();
231
232
        foreach ($data as $key => $value) {
233
            $this->setPayload($key, $value);
234
        }
235
236
        return $this;
237
    }
238
239
    /**
240
     * Has payload data.
241
     *
242
     * @param string $key
243
     *
244
     * @throws \InvalidArgumentException
245
     *
246
     * @return bool
247
     */
248
    public function hasPayload($key)
249
    {
250
        return $this->payload->containsKey($this->composePayloadKey($key));
251
    }
252
253
    /**
254
     * Get payload data.
255
     *
256
     * @param string $key
257
     * @param mixed  $default
258
     *
259
     * @throws \InvalidArgumentException
260
     *
261
     * @return mixed
262
     */
263
    public function getPayload($key, $default = null)
264
    {
265
        $key = $this->composePayloadKey($key);
266
267
        return $this->payload->containsKey($key) ? $this->payload->get($key) : $default;
268
    }
269
270
    /**
271
     * Set payload data.
272
     *
273
     * @param string $key
274
     * @param mixed  $value
275
     *
276
     * @throws \InvalidArgumentException
277
     *
278
     * @return $this
279
     */
280
    public function setPayload($key, $value)
281
    {
282
        $key = $this->composePayloadKey($key);
283
284
        foreach (self::$reservedPayloadRegex as $keyRegex) {
285
            if (preg_match($keyRegex, $key)) {
286
                throw new \InvalidArgumentException(sprintf(
287
                    '"%s" can not be used as message payload key, starts with or contains "%s"',
288
                    $key,
289
                    preg_replace('![/^$]!', '', $keyRegex)
290
                ));
291
            }
292
        }
293
294
        $this->payload->set($key, $value);
295
296
        return $this;
297
    }
298
299
    /**
300
     * Compose payload key with prefix.
301
     *
302
     * @param string $key
303
     *
304
     * @throws \InvalidArgumentException
305
     *
306
     * @return string
307
     */
308
    protected function composePayloadKey($key)
309
    {
310
        $key = $this->payloadPrefix . trim($key);
311
312
        if ($key === '') {
313
            throw new \InvalidArgumentException('Message payload parameter can not be empty');
314
        }
315
316
        return $key;
317
    }
318
}
319