Completed
Branch v1 (ffe92e)
by Julián
02:44
created

Message   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 196
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setTitle() 0 6 1
A setBody() 0 6 1
A getPayloadData() 0 4 1
A setPayloadData() 0 10 2
A hasPayload() 0 4 1
A getPayload() 0 4 2
A setPayload() 0 18 3
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
21
    /**
22
     * List of reserved payload data.
23
     *
24
     * @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG
25
     *          /Chapters/TheNotificationPayload.html
26
     * @see https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json
27
     *
28
     * @var array
29
     */
30
    protected static $reservedRegex = [
31
        // APNS
32
        '/^apc$/',
33
34
        // GCM
35
        '/^(google|gcm)/',
36
        '/^from$/',
37
        '/^collapse_key$/',
38
        '/^delay_while_idle$/',
39
        '/^time_to_live$/',
40
        '/^restricted_package_name$/',
41
        '/^dry_run$/',
42
        '/^priority$/',
43
        '/^content_available$/',
44
    ];
45
46
    /**
47
     * Default message parameters.
48
     *
49
     * @see self::$reserverRegex
50
     *
51
     * @var array
52
     */
53
    protected $defaultParameters = [
54
        'title' => null,
55
        'body' => null,
56
57
        // APNS
58
        //'launch_image' => null,
59
        //'action_loc_key' => null,
60
        //'title_loc_key' => null,
61
        //'title_loc_args' => null,
62
        //'loc_key' => null,
63
        //'loc_args' => null,
64
65
        // GCM
66
        //'icon' => null,
67
        //'sound' => 'default',
68
        //'tag' => null,
69
        //'color' => '#rrggbb',
70
        //'click_action' => null,
71
        //'title_loc_key' => null,
72
        //'title_loc_args' => null,
73
        //'body_loc_key' => null,
74
        //'body_loc_args' => null,
75
    ];
76
77
    /**
78
     * Data payload.
79
     *
80
     * @var \Doctrine\Common\Collections\ArrayCollection
81
     */
82
    protected $payload;
83
84
    /**
85
     * Constructor.
86
     *
87
     * @param array $parameters
88
     * @param array $payload
89
     */
90
    public function __construct(array $parameters = [], array $payload = [])
91
    {
92
        $this->setParameters(array_merge($this->defaultParameters, $parameters));
93
94
        $this->payload = new ArrayCollection;
95
96
        $this->setPayloadData($payload);
97
    }
98
99
    /**
100
     * Convenience method to set message title.
101
     *
102
     * @param string $title
103
     *
104
     * @throws \InvalidArgumentException
105
     *
106
     * @return $this
107
     */
108
    public function setTitle($title)
109
    {
110
        $this->setParameter('title', $title);
111
112
        return $this;
113
    }
114
115
    /**
116
     * Convenience method to set message body.
117
     *
118
     * @param string $body
119
     *
120
     * @throws \InvalidArgumentException
121
     *
122
     * @return $this
123
     */
124
    public function setBody($body)
125
    {
126
        $this->setParameter('body', $body);
127
128
        return $this;
129
    }
130
131
    /**
132
     * Get payload data.
133
     *
134
     * @return array
135
     */
136
    public function getPayloadData()
137
    {
138
        return $this->payload->toArray();
139
    }
140
141
    /**
142
     * Set payload data.
143
     *
144
     * @param array $data
145
     *
146
     * @return $this
147
     */
148
    public function setPayloadData(array $data)
149
    {
150
        $this->payload->clear();
151
152
        foreach ($data as $payload => $value) {
153
            $this->setPayload($payload, $value);
154
        }
155
156
        return $this;
157
    }
158
159
    /**
160
     * Has payload data.
161
     *
162
     * @param string $payload
163
     *
164
     * @return bool
165
     */
166
    public function hasPayload($payload)
167
    {
168
        return $this->payload->containsKey($payload);
169
    }
170
171
    /**
172
     * Get payload data.
173
     *
174
     * @param string $payload
175
     * @param mixed  $default
176
     *
177
     * @return mixed
178
     */
179
    public function getPayload($payload, $default = null)
180
    {
181
        return $this->payload->containsKey($payload) ? $this->payload->get($payload) : $default;
182
    }
183
184
    /**
185
     * Set payload data.
186
     *
187
     * @param string $payload
188
     * @param mixed  $value
189
     *
190
     * @throws \InvalidArgumentException
191
     *
192
     * @return $this
193
     */
194
    public function setPayload($payload, $value)
195
    {
196
        $payload = trim($payload);
197
198
        foreach (self::$reservedRegex as $reservedRegex) {
199
            if (preg_match($reservedRegex, $payload)) {
200
                throw new \InvalidArgumentException(sprintf(
201
                    '"%s" can not be used as custom data, starts or contains the reserved string "%s"',
202
                    $payload,
203
                    preg_replace('![/^$]!', '', $reservedRegex)
204
                ));
205
            }
206
        }
207
208
        $this->payload->set($payload, $value);
209
210
        return $this;
211
    }
212
}
213