Completed
Push — master ( 24c6fe...cc1b0b )
by Edgar
02:01
created

GCMNotification   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 4
dl 0
loc 129
ccs 60
cts 60
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A send() 0 15 3
A sendMessage() 0 8 1
A createPayload() 0 17 2
A getTokens() 0 13 3
A enqueue() 0 18 4
A getConnectionParams() 0 9 1
1
<?php
2
namespace nstdio\notymo;
3
4
/**
5
 * Class GCMNotificationComponent
6
 */
7
class GCMNotification extends AbstractNotification
8
{
9
    /**
10
     *
11
     */
12
    const GCM_HOST = 'https://gcm-http.googleapis.com/gcm/send';
13
14
    /**
15
     *
16
     */
17
    const MAX_TOKENS = 1000;
18
19
    /**
20
     * @var array
21
     */
22
    private $headers;
23
24
    /**
25
     * GCMNotificationComponent constructor.
26
     *
27
     * @param $apiKey
28
     */
29 2
    public function __construct($apiKey)
30
    {
31 2
        parent::__construct();
32
33 2
        $this->headers = array(
34 2
            'Authorization: key=' . $apiKey,
35 2
            'Content-Type: application/json',
36
        );
37 2
    }
38
39
    /**
40
     *
41
     */
42 2
    public function send()
43
    {
44 2
        if ($this->messageQueue->isEmpty()) {
45 1
            return;
46
        }
47
48 1
        $this->openConnection();
49
50
        /** @var MessageInterface $message */
51 1
        foreach ($this->messageQueue as $message) {
52 1
            $this->sendMessage($message);
53 1
        }
54
55 1
        $this->stream->close();
56 1
    }
57
58
    /**
59
     * @param $message
60
     */
61 1
    private function sendMessage($message)
62
    {
63 1
        $data = $this->createPayload($message);
64
65 1
        $this->stream->write(CURLOPT_POSTFIELDS, json_encode($data));
66
67 1
        return $this->stream->read();
68
    }
69
70
    /**
71
     * @param MessageInterface $message
72
     *
73
     * @return array
74
     */
75 1
    final protected function createPayload(MessageInterface $message)
76
    {
77
        $data = array(
78 1
            'registration_ids' => $this->getTokens($message),
79
            'data'             => array(
80 1
                'message' => $message->getMessage(),
81 1
                'sound'   => $message->getSound(),
82 1
                'badge'   => $message->getBadge(),
83 1
            ),
84 1
        );
85 1
        $customData = $message->getCustomData();
86 1
        if (!empty($customData)) {
87 1
            $data['data'] = array_merge($data['data'], $customData);
88 1
        }
89
90 1
        return $data;
91
    }
92
93 1
    private function getTokens(MessageInterface $message)
94
    {
95 1
        $ret = array();
96 1
        if ($message->isMultiple()) {
97 1
            foreach ($message->getToken() as $token) {
98 1
                $ret[] = $token;
99 1
            }
100 1
        } else {
101 1
            $ret[] = $message->getToken();
102
        }
103
104 1
        return $ret;
105
    }
106
107 1
    public function enqueue(MessageInterface $message)
108
    {
109 1
        if ($message->isMultiple() && count($message->getToken()) > self::MAX_TOKENS) {
110 1
            $parts = array_chunk($message->getToken(), self::MAX_TOKENS);
111 1
            $message->setToken(null);
112 1
            $firstElement = array_shift($parts);
113
114 1
            foreach ($parts as $item) {
115 1
                $clonedMessage = clone $message;
116 1
                $clonedMessage->setToken($item);
117 1
                parent::enqueue($clonedMessage);
118 1
            }
119
120 1
            $message->setToken($firstElement);
121 1
        }
122
123 1
        parent::enqueue($message);
124 1
    }
125
126 1
    protected function getConnectionParams()
127
    {
128
        return array(
129 1
            CURLOPT_URL            => self::GCM_HOST,
130 1
            CURLOPT_POST           => true,
131 1
            CURLOPT_HTTPHEADER     => $this->headers,
132 1
            CURLOPT_RETURNTRANSFER => true,
133 1
        );
134
    }
135
}