Response::initializeByString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3
Metric Value
dl 0
loc 10
rs 9.4285
ccs 9
cts 9
cp 1
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
/*
3
 * Copyright 2015 Alexey Maslov <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace alxmsl\Google\GCM\Response;
19
20
use alxmsl\Google\GCM\Exception\GCMFormatException;
21
use alxmsl\Google\GCM\Message\PayloadMessage;
22
use alxmsl\Google\InitializationInterface;
23
24
/**
25
 * GCM message sending response class
26
 * @author alxmsl
27
 * @date 5/28/13
28
 */ 
29
final class Response implements InitializationInterface {
30
    /**
31
     * @var int content type identifier for payload data serialization
32
     */
33
    public static $type = PayloadMessage::TYPE_PLAIN;
34
35
    /**
36
     * @var string multicast identifier
37
     */
38
    private $multicastId = '';
39
40
    /**
41
     * @var int success messages count
42
     */
43
    private $successCount = 0;
44
45
    /**
46
     * @var int failure messages count
47
     */
48
    private $failureCount = 0;
49
50
    /**
51
     * @var int count of found devices with canonical identifiers
52
     */
53
    private $canonicalIdsCount = 0;
54
55
    /**
56
     * @var Status[] result statuses
57
     */
58
    private $results = [];
59
60
    /**
61
     * Count of canonical identifiers setter
62
     * @param int $canonicalIdsCount count of canonical identifiers
63
     * @return Response self
64
     */
65 1
    public function setCanonicalIdsCount($canonicalIdsCount) {
66 1
        $this->canonicalIdsCount = (int) $canonicalIdsCount;
67 1
        return $this;
68
    }
69
70
    /**
71
     * Getter for count of canonical identifiers
72
     * @return int count of canonical identifiers
73
     */
74 4
    public function getCanonicalIdsCount() {
75 4
        return $this->canonicalIdsCount;
76
    }
77
78
    /**
79
     * Count of failure messages setter
80
     * @param int $failureCount failure messages count
81
     * @return Response self
82
     */
83 1
    public function setFailureCount($failureCount) {
84 1
        $this->failureCount = (int) $failureCount;
85 1
        return $this;
86
    }
87
88
    /**
89
     * Count of failure messages getter
90
     * @return int failure messages getter
91
     */
92 4
    public function getFailureCount() {
93 4
        return $this->failureCount;
94
    }
95
96
    /**
97
     * Multicast identifier setter
98
     * @param string $multicastId multicast identifier
99
     * @return Response self
100
     */
101 1
    public function setMulticastId($multicastId) {
102 1
        $this->multicastId = (string) $multicastId;
103 1
        return $this;
104
    }
105
106
    /**
107
     * Multicast identifier getter
108
     * @return string multicast identifier
109
     */
110 2
    public function getMulticastId() {
111 2
        return $this->multicastId;
112
    }
113
114
    /**
115
     * Count of success messages setter
116
     * @param int $successCount success messages count
117
     * @return Response self
118
     */
119 1
    public function setSuccessCount($successCount) {
120 1
        $this->successCount = (int) $successCount;
121 1
        return $this;
122
    }
123
124
    /**
125
     * Count of success messages getter
126
     * @return int count of success messages
127
     */
128 4
    public function getSuccessCount() {
129 4
        return $this->successCount;
130
    }
131
132
    /**
133
     * Result statuses getter
134
     * @return Status[] result statuses
135
     */
136 5
    public function getResults() {
137 5
        return $this->results;
138
    }
139
140
    /**
141
     * Initialization method
142
     * @param string $string data for object initialization
143
     * @return $this initialized object
144
     * @throws GCMFormatException for unknown response format
145
     */
146 5
    public static function initializeByString($string) {
147 5
        switch (self::$type) {
148 5
            case PayloadMessage::TYPE_PLAIN:
149 2
                return self::createPlainResponse($string);
150 3
            case PayloadMessage::TYPE_JSON:
151 2
                return self::createJsonResponse($string);
152 1
            default:
153 1
                throw new GCMFormatException('unsupported response format code \'' . self::$type . '\'');
154 1
        }
155
    }
156
157
    /**
158
     * Create response object by plain text data
159
     * @param string $string plain text data
160
     * @return Response self
161
     * @throws GCMFormatException when plain text data format was not supported
162
     */
163 2
    private static function createPlainResponse($string) {
164 2
        $parts = explode("\n", $string);
165 2
        $firstLine = explode('=', $parts[0]);
166
167 2
        $Response = new self();
168 2
        $Status = new Status();
169 2
        if (isset($parts[1])) {
170 2
            $secondLine = explode('=', $parts[1]);
171 2
            if ($secondLine[0] == 'registration_id') {
172 2
                $Response->canonicalIdsCount = 1;
173 2
                $Status->setCanonicalId($secondLine[1]);
174 2
            } else {
175 1
                throw new GCMFormatException('unknown second line response field \'' . $firstLine[0] . '\'');
176
            }
177 2
        }
178 2
        switch ($firstLine[0]) {
179 2
            case 'id':
180 2
                $Response->successCount = 1;
181 2
                $Response->failureCount = 0;
182 2
                $Status->setMessageId($firstLine[1]);
183 2
                break;
184 1
            case 'Error':
185 1
                $Response->successCount = 0;
186 1
                $Response->failureCount = 1;
187 1
                $Status->setError($firstLine[1]);
188 1
                break;
189 1
            default:
190 1
                throw new GCMFormatException('unknown first line response field \'' . $firstLine[0] . '\'');
191 2
        }
192 2
        $Response->results[] = $Status;
193 2
        return $Response;
194
    }
195
196
    /**
197
     * Create response object by JSON data
198
     * @param string $string JSON response data
199
     * @return Response self
200
     */
201 2
    private static function createJsonResponse($string) {
202 2
        $Data = json_decode($string);
203 2
        $Response = new self();
204 2
        $Response->canonicalIdsCount = (int) $Data->canonical_ids;
205 2
        $Response->failureCount = (int) $Data->failure;
206 2
        $Response->multicastId = (string) $Data->multicast_id;
207 2
        $Response->successCount = (int) $Data->success;
208
209 2
        foreach ($Data->results as $Result) {
210 2
            $Status = new Status();
211 2
            if (isset($Result->message_id)) {
212 2
                $Status->setMessageId($Result->message_id);
213 2
            }
214 2
            if (isset($Result->registration_id)) {
215 2
                $Status->setCanonicalId($Result->registration_id);
216 2
            }
217 2
            if (isset($Result->error)) {
218 2
                $Status->setError($Result->error);
219 2
            }
220 2
            $Response->results[] = $Status;
221 2
        }
222 2
        return $Response;
223
    }
224
}
225