1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\NotificationService\GCM; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\NotificationService\ServiceClientBase; |
13
|
|
|
use Zbox\UnifiedPush\Exception\ClientException; |
14
|
|
|
use Buzz\Browser; |
15
|
|
|
use Buzz\Client\MultiCurl; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ServiceClient |
19
|
|
|
* @package Zbox\UnifiedPush\NotificationService\GCM |
20
|
|
|
*/ |
21
|
|
|
class ServiceClient extends ServiceClientBase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Initializing HTTP client |
25
|
|
|
* |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
|
|
protected function createClient() |
29
|
|
|
{ |
30
|
|
|
$client = new MultiCurl(); |
31
|
|
|
$client->setVerifyPeer(false); |
32
|
|
|
|
33
|
|
|
$this->serviceClient = new Browser($client); |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* When the message is processed successfully, the HTTP response has a 200 status. |
40
|
|
|
* Body contains more information about the status of the message. When the request is rejected, |
41
|
|
|
* the HTTP response contains a non-200 status code. |
42
|
|
|
* |
43
|
|
|
* @throws ClientException |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function sendRequest() |
47
|
|
|
{ |
48
|
|
|
$notification = $this->getNotificationOrThrowException(); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$connection = $this->getClientConnection(); |
52
|
|
|
$serviceURL = $this->getServiceURL(); |
53
|
|
|
$credentials = $this->getCredentials(); |
54
|
|
|
|
55
|
|
|
$headers[] = 'Authorization: key='.$credentials->getAuthToken(); |
|
|
|
|
56
|
|
|
$headers[] = 'Content-Type: application/json'; |
57
|
|
|
|
58
|
|
|
$response = $connection->post($serviceURL['url'], $headers, $notification['body']); |
59
|
|
|
$connection->getClient()->flush(); |
60
|
|
|
|
61
|
|
|
} catch (\Exception $e) { |
62
|
|
|
throw new ClientException($e->getMessage()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
new Response($response, $notification['recipients']); |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.