GcmClient::setUp()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the MobileNotif package.
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 LinkValue\MobileNotif\Client;
11
12
use LinkValue\MobileNotif\Logger\ClientLoggableTrait;
13
use LinkValue\MobileNotif\Model\Message;
14
use Psr\Log\NullLogger;
15
16
/**
17
 * GcmClient
18
 * Google Cloud Messaging implementation.
19
 *
20
 * @author  Jamal Youssefi <[email protected]>
21
 * @author  Valentin Coulon <[email protected]>
22
 */
23
class GcmClient implements ClientInterface
24
{
25
    use ClientLoggableTrait;
26
27
    /**
28
     * Push server params.
29
     *
30
     * @var array
31
     */
32
    private $params;
33
34
    /**
35
     * AndroidPushNotificationClient constructor.
36
     */
37 7
    public function __construct()
38
    {
39 7
        $this->logger = new NullLogger();
40 7
    }
41
42
    /**
43
     * Set up the parameters.
44
     *
45
     * @param array $params
46
     *
47
     * @throws \RuntimeException
48
     */
49 6
    public function setUp(array $params)
50
    {
51 6
        if (!isset($params['endpoint'])) {
52 1
            throw new \RuntimeException('Parameter "endpoint" missing.');
53
        }
54
55 5
        if (!isset($params['api_access_key'])) {
56 1
            throw new \RuntimeException('Parameter "api_access_key" missing.');
57
        }
58
59 4
        $this->params = $params;
60 4
    }
61
62
    /**
63
     * Push a notification to a mobile client.
64
     *
65
     * @param Message $message
66
     */
67 4
    public function push(Message $message)
68
    {
69 4
        if (empty($this->params)) {
70 1
            throw new \RuntimeException('Please set up this client using setUp() method before pushing messages.');
71
        }
72
73 3
        $tokens = $message->getTokens();
74 3
        if (empty($tokens)) {
75 1
            throw new \RuntimeException('No device token set. Please add at least 1 token using Message::addToken() before trying to push the message.');
76
        }
77
78 2
        $payload = $message->getPayloadAsJson();
79
80
        $headers = array(
81 2
            'Authorization: key='.$this->params['api_access_key'],
82 2
            'Content-Type: application/json',
83 2
        );
84
85 2
        $this->logger->info('Sending GcmMessage to Google Cloud Messaging server', array(
86 2
            'payload' => $payload,
87 2
        ));
88
89 2
        $ch = curl_init();
90 2
        curl_setopt($ch, CURLOPT_URL, $this->params['endpoint']);
91 2
        curl_setopt($ch, CURLOPT_POST, true);
92 2
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
93 2
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
94 2
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
95 2
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
96 2
        curl_exec($ch);
97 2
        curl_close($ch);
98 2
    }
99
}
100