Completed
Push — master ( f47fb6...c9af04 )
by Julián
02:24
created

GcmBuilder::buildPushMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 14
nc 2
nop 2
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify\Adapter\Gcm;
11
12
use Jgut\Tify\Message;
13
use Jgut\Tify\Notification;
14
use Zend\Http\Client\Adapter\Socket;
15
use Zend\Http\Client as HttpClient;
16
use ZendService\Google\Gcm\Client;
17
18
/**
19
 * Class GcmBuilder
20
 */
21
class GcmBuilder
22
{
23
    /**
24
     * Get opened push service client.
25
     *
26
     * @param string $apiKey
27
     *
28
     * @return \ZendService\Google\Gcm\Client
29
     */
30
    public function buildPushClient($apiKey)
31
    {
32
        $client = new Client;
33
        $client->setApiKey($apiKey);
34
35
        $httpClient = new HttpClient(
36
            null,
37
            [
38
                'service' => Socket::class,
39
                'strictredirects' => true,
40
                'sslverifypeer' => false,
41
            ]
42
        );
43
44
        $client->setHttpClient($httpClient);
45
46
        return $client;
47
    }
48
49
    /**
50
     * Get configured service message.
51
     *
52
     * @param array                   $tokens
53
     * @param \Jgut\Tify\Notification $notification
54
     *
55
     * @throws \ZendService\Google\Exception\InvalidArgumentException
56
     * @throws \ZendService\Google\Exception\RuntimeException
57
     *
58
     * @return \Jgut\Tify\Adapter\Gcm\GcmMessage
59
     */
60
    public function buildPushMessage(array $tokens, Notification $notification)
61
    {
62
        $message = $notification->getMessage();
63
64
        $pushMessage = new GcmMessage();
65
66
        $pushMessage
67
            ->setRegistrationIds($tokens)
68
            ->setCollapseKey($notification->getParameter('collapse_key'))
69
            ->setDelayWhileIdle($notification->getParameter('delay_while_idle'))
70
            ->setTimeToLive($notification->getParameter('time_to_live'))
71
            ->setRestrictedPackageName($notification->getParameter('restricted_package_name'))
72
            ->setDryRun($notification->getParameter('dry_run'))
73
            ->setData($message->getPayloadData());
74
75
        if ($this->shouldHaveNotification($message)) {
76
            $pushMessage->setNotificationPayload($message->getParameters());
77
        }
78
79
        return $pushMessage;
80
    }
81
82
    /**
83
     * Message should have notification data.
84
     *
85
     * @param \Jgut\Tify\Message $message
86
     *
87
     * @return bool
88
     */
89
    private function shouldHaveNotification(Message $message)
90
    {
91
        static $notificationParams = [
92
            'title',
93
            'body',
94
            'icon',
95
            'sound',
96
            'tag',
97
            'color',
98
            'click_action',
99
            'title_loc_key',
100
            'title_loc_args',
101
            'body_loc_key',
102
            'body_loc_args',
103
        ];
104
105
        foreach ($notificationParams as $parameter) {
106
            if ($message->hasParameter($parameter)) {
107
                return true;
108
            }
109
        }
110
111
        return false;
112
    }
113
}
114