Completed
Branch 2.x (c99d86)
by Julián
08:01
created

Message   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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