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

GcmMessageBuilder::build()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 14
nc 2
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\Service\Message;
11
12
use Jgut\Tify\Notification\GcmNotification;
13
use Jgut\Tify\Service\Message\Gcm as ServiceMessage;
14
15
/**
16
 * Class GcmMessageBuilder
17
 */
18
class GcmMessageBuilder
19
{
20
    /**
21
     * Get configured service message.
22
     *
23
     * @param array                                   $tokens
24
     * @param \Jgut\Tify\Notification\GcmNotification $notification
25
     *
26
     * @throws \InvalidArgumentException
27
     * @throws \RuntimeException
28
     *
29
     * @return \Jgut\Tify\Service\Message\Gcm
30
     */
31
    public static function build(array $tokens, GcmNotification $notification)
32
    {
33
        $message = $notification->getMessage();
34
35
        $pushMessage = new ServiceMessage();
36
37
        $pushMessage
38
            ->setRegistrationIds($tokens)
39
            ->setCollapseKey($notification->getOption('collapse_key'))
40
            ->setDelayWhileIdle($notification->getOption('delay_while_idle'))
41
            ->setTimeToLive($notification->getOption('time_to_live'))
42
            ->setRestrictedPackageName($notification->getOption('restricted_package_name'))
43
            ->setDryRun($notification->getOption('dry_run'))
44
            ->setData($message->getParameters());
45
46
        if ($message->getOption('title') !== null || $message->getOption('body') !== null) {
47
            $pushMessage->setNotificationPayload($message->getOptions());
48
        }
49
50
        return $pushMessage;
51
    }
52
}
53