ApnsBuilder::buildPushMessage()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 8.6315
c 0
b 0
f 0
cc 6
eloc 34
nc 32
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\Apns;
11
12
use Jgut\Tify\Exception\AdapterException;
13
use Jgut\Tify\Message;
14
use Jgut\Tify\Notification;
15
use Jgut\Tify\Receiver\ApnsReceiver;
16
use ZendService\Apple\Apns\Client\AbstractClient;
17
use ZendService\Apple\Apns\Client\Feedback as FeedbackClient;
18
use ZendService\Apple\Apns\Client\Message as MessageClient;
19
use ZendService\Apple\Apns\Message\Alert as ServiceMessageAlert;
20
use ZendService\Apple\Apns\Message as ServiceMessage;
21
22
/**
23
 * Class ApnsBuilder
24
 */
25
class ApnsBuilder
26
{
27
    /**
28
     * Get opened push service client.
29
     *
30
     * @param string $certificate
31
     * @param string $passPhrase
32
     * @param bool   $sandbox
33
     *
34
     * @throws \Jgut\Tify\Exception\AdapterException
35
     *
36
     * @return \ZendService\Apple\Apns\Client\Message
37
     */
38
    public function buildPushClient($certificate, $passPhrase = '', $sandbox = false)
39
    {
40
        return $this->buildClient(new MessageClient, $certificate, $passPhrase, $sandbox);
41
    }
42
43
    /**
44
     * Get opened feedback service client.
45
     *
46
     * @param string $certificate
47
     * @param string $passPhrase
48
     * @param bool   $sandbox
49
     *
50
     * @throws \Jgut\Tify\Exception\AdapterException
51
     *
52
     * @return \ZendService\Apple\Apns\Client\Feedback
53
     */
54
    public function buildFeedbackClient($certificate, $passPhrase = '', $sandbox = false)
55
    {
56
        return $this->buildClient(new FeedbackClient, $certificate, $passPhrase, $sandbox);
57
    }
58
59
    /**
60
     * Get opened client.
61
     *
62
     * @param \ZendService\Apple\Apns\Client\AbstractClient $client
63
     * @param string                                        $certificate
64
     * @param string                                        $passPhrase
65
     * @param bool                                          $sandbox
66
     *
67
     * @throws \Jgut\Tify\Exception\AdapterException
68
     *
69
     * @return \ZendService\Apple\Apns\Client\AbstractClient
70
     *
71
     * @codeCoverageIgnore
72
     */
73
    protected function buildClient(AbstractClient $client, $certificate, $passPhrase = '', $sandbox = false)
74
    {
75
        try {
76
            $client->open(
77
                (bool) $sandbox ? AbstractClient::SANDBOX_URI : AbstractClient::PRODUCTION_URI,
78
                $certificate,
79
                $passPhrase
80
            );
81
        } catch (\Exception $exception) {
82
            throw new AdapterException($exception->getMessage(), $exception->getCode(), $exception);
83
        }
84
85
        return $client;
86
    }
87
88
    /**
89
     * Get service message from origin.
90
     *
91
     * @param \Jgut\Tify\Receiver\ApnsReceiver $receiver
92
     * @param \Jgut\Tify\Notification          $notification
93
     *
94
     * @throws \ZendService\Apple\Exception\RuntimeException
95
     *
96
     * @return \ZendService\Apple\Apns\Message
97
     */
98
    public function buildPushMessage(ApnsReceiver $receiver, Notification $notification)
99
    {
100
        $message = $notification->getMessage();
101
102
        $messageId = sha1(
103
            sprintf(
104
                '%s%s%s%s',
105
                $receiver->getToken(),
106
                $message->getParameter('title'),
107
                $message->getParameter('body'),
108
                time()
109
            )
110
        );
111
        $badge = $notification->getParameter('badge') === null ? null : (int) $notification->getParameter('badge');
112
113
        $pushMessage = (new ServiceMessage())
114
            ->setId($messageId)
115
            ->setToken($receiver->getToken())
116
            ->setBadge($badge)
117
            ->setSound($notification->getParameter('sound'))
118
            ->setCategory($notification->getParameter('category'))
119
            ->setCustom($message->getPayloadData());
120
121
        if ($notification->getParameter('content-available') !== null) {
122
            $pushMessage->setContentAvailable((int) $notification->getParameter('content-available'));
123
        }
124
125
        if (is_array($notification->getParameter('url-args'))) {
126
            $pushMessage->setUrlArgs($notification->getParameter('url-args'));
127
        }
128
129
        if ($notification->getParameter('expire') !== null) {
130
            $pushMessage->setExpire($notification->getParameter('expire'));
131
        }
132
133
        if ($this->shouldHaveAlert($message)) {
134
            $pushMessage->setAlert(new ServiceMessageAlert(
135
                $message->getParameter('body'),
136
                $message->getParameter('action-loc-key'),
137
                $message->getParameter('loc-key'),
138
                $message->getParameter('loc-args'),
139
                $message->getParameter('launch-image'),
140
                $message->getParameter('title'),
141
                $message->getParameter('title-loc-key'),
142
                $message->getParameter('title-loc-args')
143
            ));
144
        }
145
146
        return $pushMessage;
147
    }
148
149
    /**
150
     * Message should have alert dictionary.
151
     *
152
     * @param \Jgut\Tify\Message $message
153
     *
154
     * @return bool
155
     */
156
    private function shouldHaveAlert(Message $message)
157
    {
158
        static $alertParams = [
159
            'title',
160
            'body',
161
            'title-loc-key',
162
            'title-loc-args',
163
            'loc-key',
164
            'loc-args',
165
            'action-loc-key',
166
            'launch-image',
167
        ];
168
169
        foreach ($alertParams as $parameter) {
170
            if ($message->hasParameter($parameter)) {
171
                return true;
172
            }
173
        }
174
175
        return false;
176
    }
177
}
178