Completed
Push — master ( 020c5c...9dbce5 )
by Julián
08:49
created

ApnsBuilder::buildPushMessage()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 49
rs 6.1403
cc 8
eloc 34
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\Notification;
14
use Jgut\Tify\Receiver\ApnsReceiver;
15
use ZendService\Apple\Apns\Client\AbstractClient;
16
use ZendService\Apple\Apns\Client\Feedback as FeedbackClient;
17
use ZendService\Apple\Apns\Client\Message as MessageClient;
18
use ZendService\Apple\Apns\Message\Alert as ServiceMessageAlert;
19
use ZendService\Apple\Apns\Message as ServiceMessage;
20
21
/**
22
 * Class ApnsBuilder
23
 */
24
class ApnsBuilder
25
{
26
    /**
27
     * Get opened push service client.
28
     *
29
     * @param string $certificate
30
     * @param string $passPhrase
31
     * @param bool   $sandbox
32
     *
33
     * @throws \Jgut\Tify\Exception\AdapterException
34
     *
35
     * @return \ZendService\Apple\Apns\Client\Message
36
     */
37
    public function buildPushClient($certificate, $passPhrase = '', $sandbox = false)
38
    {
39
        return $this->buildClient(new MessageClient, $certificate, $passPhrase, $sandbox);
40
    }
41
42
    /**
43
     * Get opened feedback service client.
44
     *
45
     * @param string $certificate
46
     * @param string $passPhrase
47
     * @param bool   $sandbox
48
     *
49
     * @throws \Jgut\Tify\Exception\AdapterException
50
     *
51
     * @return \ZendService\Apple\Apns\Client\Feedback
52
     */
53
    public function buildFeedbackClient($certificate, $passPhrase = '', $sandbox = false)
54
    {
55
        return $this->buildClient(new FeedbackClient, $certificate, $passPhrase, $sandbox);
56
    }
57
58
    /**
59
     * Get opened client.
60
     *
61
     * @param \ZendService\Apple\Apns\Client\AbstractClient $client
62
     * @param string                                        $certificate
63
     * @param string                                        $passPhrase
64
     * @param bool                                          $sandbox
65
     *
66
     * @throws \Jgut\Tify\Exception\AdapterException
67
     *
68
     * @return \ZendService\Apple\Apns\Client\AbstractClient
69
     *
70
     * @codeCoverageIgnore
71
     */
72
    protected function buildClient(AbstractClient $client, $certificate, $passPhrase = '', $sandbox = false)
73
    {
74
        try {
75
            $client->open(
76
                (bool) $sandbox ? AbstractClient::SANDBOX_URI : AbstractClient::PRODUCTION_URI,
77
                $certificate,
78
                $passPhrase
79
            );
80
        } catch (\Exception $exception) {
81
            throw new AdapterException($exception->getMessage(), $exception->getCode(), $exception);
82
        }
83
84
        return $client;
85
    }
86
87
    /**
88
     * Get service message from origin.
89
     *
90
     * @param \Jgut\Tify\Receiver\ApnsReceiver $receiver
91
     * @param \Jgut\Tify\Notification          $notification
92
     *
93
     * @throws \ZendService\Apple\Exception\RuntimeException
94
     *
95
     * @return \ZendService\Apple\Apns\Message
96
     */
97
    public function buildPushMessage(ApnsReceiver $receiver, Notification $notification)
98
    {
99
        $message = $notification->getMessage();
100
101
        $messageId = sha1(
102
            sprintf(
103
                '%s%s%s%s',
104
                $receiver->getToken(),
105
                $message->getParameter('title'),
106
                $message->getParameter('body'),
107
                time()
108
            )
109
        );
110
        $badge = (int) $notification->getParameter('badge') === 0 ? null : (int) $notification->getParameter('badge');
111
112
        $pushMessage = (new ServiceMessage())
113
            ->setId($messageId)
114
            ->setToken($receiver->getToken())
115
            ->setBadge($badge)
116
            ->setSound($notification->getParameter('sound'))
117
            ->setContentAvailable($notification->getParameter('content_available'))
118
            ->setCategory($notification->getParameter('category'))
119
            ->setCustom($message->getPayloadData());
120
121
        if (is_array($notification->getParameter('url-args'))) {
122
            $pushMessage->setUrlArgs($notification->getParameter('url-args'));
123
        }
124
125
        if ($notification->getParameter('expire') !== null) {
126
            $pushMessage->setExpire($notification->getParameter('expire'));
127
        }
128
129
        if ($message->getParameter('title') !== null || $message->getParameter('body') !== null
130
            || $message->getParameter('title_loc_key') !== null || $message->getParameter('loc_key') !== null
131
        ) {
132
            $pushMessage->setAlert(new ServiceMessageAlert(
133
                $message->getParameter('body'),
134
                $message->getParameter('action_loc_key'),
135
                $message->getParameter('loc_key'),
136
                $message->getParameter('loc_args'),
137
                $message->getParameter('launch_image'),
138
                $message->getParameter('title'),
139
                $message->getParameter('title_loc_key'),
140
                $message->getParameter('title_loc_args')
141
            ));
142
        }
143
144
        return $pushMessage;
145
    }
146
}
147