GcmMessage   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 128
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getNotificationPayload() 0 4 1
A setNotificationPayload() 0 10 2
A addNotificationPayload() 0 16 3
A clearNotificationPayload() 0 6 1
C toJson() 0 27 7
A getCompoundPayload() 0 13 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\Adapter\Gcm;
11
12
use Zend\Json\Json;
13
use ZendService\Google\Exception\InvalidArgumentException;
14
use ZendService\Google\Exception\RuntimeException;
15
use ZendService\Google\Gcm\Message;
16
17
/**
18
 * Custom GCM service message.
19
 *
20
 * Implements notification payload parameters.
21
 */
22
class GcmMessage extends Message
23
{
24
    const DEFAULT_TTL = 2419200; // 4 weeks
25
26
    /**
27
     * @var array
28
     */
29
    protected $notificationPayload = [];
30
31
    /**
32
     * Get notification payload data.
33
     *
34
     * @return array
35
     */
36
    public function getNotificationPayload()
37
    {
38
        return $this->notificationPayload;
39
    }
40
41
    /**
42
     * Set notification payload data.
43
     *
44
     * @param array $payload
45
     *
46
     * @throws \ZendService\Google\Exception\InvalidArgumentException
47
     * @throws \ZendService\Google\Exception\RuntimeException
48
     *
49
     * @return $this
50
     */
51
    public function setNotificationPayload(array $payload)
52
    {
53
        $this->clearNotificationPayload();
54
55
        foreach ($payload as $key => $value) {
56
            $this->addNotificationPayload($key, $value);
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * Add notification payload data.
64
     *
65
     * @param string $key
66
     * @param mixed  $value
67
     *
68
     * @throws \ZendService\Google\Exception\InvalidArgumentException
69
     * @throws \ZendService\Google\Exception\RuntimeException
70
     *
71
     * @return $this
72
     */
73
    public function addNotificationPayload($key, $value)
74
    {
75
        $key = trim($key);
76
77
        if ($key === '') {
78
            throw new InvalidArgumentException('Notification payload key must be a non-empty string');
79
        }
80
81
        if (array_key_exists($key, $this->notificationPayload)) {
82
            throw new RuntimeException(sprintf('"%s" conflicts with current set notification payload data', $key));
83
        }
84
85
        $this->notificationPayload[$key] = $value;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Clear notification payload data.
92
     */
93
    public function clearNotificationPayload()
94
    {
95
        $this->notificationPayload = [];
96
97
        return $this;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function toJson()
104
    {
105
        $json = [];
106
107
        if (count($this->registrationIds)) {
108
            $json['registration_ids'] = $this->registrationIds;
109
        }
110
        if ($this->collapseKey) {
111
            $json['collapse_key'] = $this->collapseKey;
112
        }
113
        if ($this->delayWhileIdle) {
114
            $json['delay_while_idle'] = $this->delayWhileIdle;
115
        }
116
        if ($this->timeToLive !== self::DEFAULT_TTL) {
117
            $json['time_to_live'] = $this->timeToLive;
118
        }
119
        if ($this->restrictedPackageName) {
120
            $json['restricted_package_name'] = $this->restrictedPackageName;
121
        }
122
        if ($this->dryRun) {
123
            $json['dry_run'] = $this->dryRun;
124
        }
125
126
        $json = array_merge($json, $this->getCompoundPayload());
127
128
        return Json::encode($json);
129
    }
130
131
    /**
132
     * Retrieve payload.
133
     *
134
     * @return array
135
     */
136
    protected function getCompoundPayload()
137
    {
138
        $payload = [];
139
140
        if (count($this->data)) {
141
            $payload['data'] = $this->data;
142
        }
143
        if (count($this->notificationPayload)) {
144
            $payload['notification'] = $this->notificationPayload;
145
        }
146
147
        return $payload;
148
    }
149
}
150