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

ApnsBuilder::buildPushMessage()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 47
rs 8.5125
cc 5
eloc 33
nc 16
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
            ->setContentAvailable((int) $notification->getParameter('content-available'))
119
            ->setCategory($notification->getParameter('category'))
120
            ->setCustom($message->getPayloadData());
121
122
        if (is_array($notification->getParameter('url-args'))) {
123
            $pushMessage->setUrlArgs($notification->getParameter('url-args'));
124
        }
125
126
        if ($notification->getParameter('expire') !== null) {
127
            $pushMessage->setExpire($notification->getParameter('expire'));
128
        }
129
130
        if ($this->shouldHaveAlert($message)) {
131
            $pushMessage->setAlert(new ServiceMessageAlert(
132
                $message->getParameter('body'),
133
                $message->getParameter('action-loc-key'),
134
                $message->getParameter('loc-key'),
135
                $message->getParameter('loc-args'),
136
                $message->getParameter('launch-image'),
137
                $message->getParameter('title'),
138
                $message->getParameter('title-loc-key'),
139
                $message->getParameter('title-loc-args')
140
            ));
141
        }
142
143
        return $pushMessage;
144
    }
145
146
    /**
147
     * Message should have alert dictionary.
148
     *
149
     * @param \Jgut\Tify\Message $message
150
     *
151
     * @return bool
152
     */
153
    private function shouldHaveAlert(Message $message)
154
    {
155
        static $alertParams = [
156
            'title',
157
            'body',
158
            'title-loc-key',
159
            'title-loc-args',
160
            'loc-key',
161
            'loc-args',
162
            'action-loc-key',
163
            'launch-image'
164
        ];
165
166
        foreach ($alertParams as $parameter) {
167
            if ($message->hasParameter($parameter)) {
168
                return true;
169
            }
170
        }
171
172
        return false;
173
    }
174
}
175