Completed
Push — master ( 613542...020c5c )
by Julián
03:39
created

ApnsMessageBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 52
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 38 5
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\Service\Message;
11
12
use Jgut\Tify\Recipient\ApnsRecipient;
13
use Jgut\Tify\Notification\ApnsNotification;
14
use ZendService\Apple\Apns\Message as ServiceMessage;
15
use ZendService\Apple\Apns\Message\Alert as ServiceMessageAlert;
16
17
/**
18
 * Class ApnsMessageBuilder
19
 */
20
class ApnsMessageBuilder
21
{
22
    /**
23
     * Get service message from origin.
24
     *
25
     * @param \Jgut\Tify\Recipient\ApnsRecipient       $recipient
26
     * @param \Jgut\Tify\Notification\ApnsNotification $notification
27
     *
28
     * @throws \InvalidArgumentException
29
     * @throws \RuntimeException
30
     *
31
     * @return \ZendService\Apple\Apns\Message
32
     */
33
    public static function build(ApnsRecipient $recipient, ApnsNotification $notification)
34
    {
35
        $message = $notification->getMessage();
36
37
        $badge = ((int) $notification->getOption('badge', 0) === 0)
38
            ? null
39
            : $notification->getOption('badge') + (int) $recipient->getParameter('badge', 0);
40
41
        $pushMessage = new ServiceMessage();
42
43
        $pushMessage
44
            ->setId(sha1($recipient->getToken() . $message->getOption('body')))
45
            ->setToken($recipient->getToken())
46
            ->setSound($notification->getOption('sound'))
47
            ->setContentAvailable($notification->getOption('content_available'))
48
            ->setCategory($notification->getOption('category'))
49
            ->setCustom($message->getParameters())
50
            ->setBadge($badge);
51
52
        if ($notification->getOption('expire') !== null) {
53
            $pushMessage->setExpire($notification->getOption('expire'));
54
        }
55
56
        if ($message->getOption('title') !== null || $message->getOption('body') !== null) {
57
            $pushMessage->setAlert(new ServiceMessageAlert(
58
                $message->getOption('body'),
59
                $message->getOption('action_loc_key'),
60
                $message->getOption('loc_key'),
61
                $message->getOption('loc_args'),
62
                $message->getOption('launch_image'),
63
                $message->getOption('title'),
64
                $message->getOption('title_loc_key'),
65
                $message->getOption('title_loc_args')
66
            ));
67
        }
68
69
        return $pushMessage;
70
    }
71
}
72