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

DefaultFactory::buildFeedbackClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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