Completed
Branch 2.x (59be6b)
by Julián
11:20
created

DefaultFactory::shouldAddNotification()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * Unified push notification services abstraction (http://github.com/juliangut/tify).
5
 *
6
 * @license BSD-3-Clause
7
 * @link https://github.com/juliangut/tify
8
 * @author Julián Gutiérrez <[email protected]>
9
 */
10
11
namespace Jgut\Tify\Adapter\Gcm;
12
13
use Jgut\Tify\Message;
14
use Jgut\Tify\Notification;
15
use Zend\Http\Client\Adapter\Socket;
16
use Zend\Http\Client as HttpClient;
17
use ZendService\Google\Gcm\Client as PushClient;
18
use ZendService\Google\Gcm\Message as ServiceMessage;
19
20
/**
21
 * GCM default service factory.
22
 */
23
class DefaultFactory implements Factory
24
{
25
    /**
26
     * Notification parameters list.
27
     *
28
     * @var array
29
     */
30
    protected static $notificationParams = [
31
        Message::PARAMETER_TITLE,
32
        Message::PARAMETER_BODY,
33
        Message::PARAMETER_ICON,
34
        Message::PARAMETER_SOUND,
35
        Message::PARAMETER_TAG,
36
        Message::PARAMETER_COLOR,
37
        Message::PARAMETER_CLICK_ACTION,
38
        Message::PARAMETER_TITLE_LOC_KEY,
39
        Message::PARAMETER_TITLE_LOC_ARGS,
40
        Message::PARAMETER_BODY_LOC_KEY,
41
        Message::PARAMETER_BODY_LOC_ARGS,
42
    ];
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function buildPushClient($apiKey)
48
    {
49
        $client = new PushClient;
50
        $client->setApiKey($apiKey);
51
52
        $httpClient = new HttpClient(
53
            null,
54
            [
55
                'service' => Socket::class,
56
                'strictredirects' => true,
57
                'sslverifypeer' => false,
58
            ]
59
        );
60
61
        $client->setHttpClient($httpClient);
62
63
        return $client;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @throws \ZendService\Google\Exception\InvalidArgumentException
70
     * @throws \ZendService\Google\Exception\RuntimeException
71
     */
72
    public function buildPushMessage(array $tokens, Notification $notification)
73
    {
74
        $message = $notification->getMessage();
75
76
        $pushMessage = (new ServiceMessage)
77
            ->setRegistrationIds($tokens)
78
            ->setPriority($notification->getParameter(Notification::PARAMETER_PRIORITY))
79
            ->setCollapseKey($notification->getParameter(Notification::PARAMETER_COLLAPSE_KEY))
80
            ->setDelayWhileIdle($notification->getParameter(Notification::PARAMETER_DELAY_WHILE_IDLE))
81
            ->setTimeToLive($notification->getParameter(Notification::PARAMETER_TTL))
82
            ->setRestrictedPackageName($notification->getParameter(Notification::PARAMETER_RESTRICTED_PACKAGE_NAME))
83
            ->setDryRun($notification->getParameter(Notification::PARAMETER_DRY_RUN))
84
            ->setData($message->getPayloadData());
85
86
        if ($this->shouldAddNotification($message)) {
87
            $pushMessage->setNotification($message->getParameters());
88
        }
89
90
        return $pushMessage;
91
    }
92
93
    /**
94
     * Message should have notification data.
95
     *
96
     * @param Message $message
97
     *
98
     * @return bool
99
     */
100
    private function shouldAddNotification(Message $message)
101
    {
102
        $shouldHavePayload = false;
103
104
        foreach (static::$notificationParams as $parameter) {
105
            if ($message->hasParameter($parameter)) {
106
                $shouldHavePayload = true;
107
108
                break;
109
            }
110
        }
111
112
        return $shouldHavePayload;
113
    }
114
}
115