Completed
Push — master ( b2c31d...142309 )
by Vitaliy
01:49
created

PayloadEncoder::convertApsToArray()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 32
nop 1
1
<?php
2
3
/*
4
 * This file is part of the AppleApnPush package
5
 *
6
 * (c) Vitaliy Zhuk <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Apple\ApnPush\Encoder;
13
14
use Apple\ApnPush\Model\Alert;
15
use Apple\ApnPush\Model\Aps;
16
use Apple\ApnPush\Model\Payload;
17
18
/**
19
 * The encoder for encode notification payload to string for next send to Apple Push Notification Service
20
 */
21
class PayloadEncoder implements PayloadEncoderInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function encode(Payload $payload) : string
27
    {
28
        $data = [
29
            'aps' => $this->convertApsToArray($payload->getAps()),
30
        ];
31
32
        $data = array_merge($payload->getCustomData(), $data);
33
34
        return json_encode($data);
35
    }
36
37
    /**
38
     * Convert APS data to array
39
     *
40
     * @param Aps $aps
41
     *
42
     * @return array
43
     */
44
    private function convertApsToArray(Aps $aps) : array
45
    {
46
        $data = [
47
            'alert' => $this->convertAlertToArray($aps->getAlert()),
48
        ];
49
50
        if ($aps->getSound()) {
51
            $data['sound'] = $aps->getSound();
52
        }
53
54
        if ($aps->getBadge()) {
55
            $data['badge'] = $aps->getBadge();
56
        }
57
58
        if ($aps->getCategory()) {
59
            $data['category'] = $aps->getCategory();
60
        }
61
62
        if ($aps->isContentAvailable()) {
63
            $data['content-available'] = 1;
64
        }
65
66
        if ($aps->getThreadId()) {
67
            $data['thread-id'] = $aps->getThreadId();
68
        }
69
70
        return $data;
71
    }
72
73
    /**
74
     * Convert alert object to array
75
     *
76
     * @param Alert $alert
77
     *
78
     * @return array
79
     */
80
    private function convertAlertToArray(Alert $alert) : array
81
    {
82
        $data = [];
83
84
        if ($alert->getBodyLocalized()->getKey()) {
85
            $data['loc-key'] = $alert->getBodyLocalized()->getKey();
86
            $data['loc-args'] = $alert->getBodyLocalized()->getArgs();
87
        } else {
88
            $data['body'] = $alert->getBody();
89
        }
90
91
        if ($alert->getTitleLocalized()->getKey()) {
92
            $data['title-loc-key'] = $alert->getTitleLocalized()->getKey();
93
            $data['title-loc-args'] = $alert->getTitleLocalized()->getArgs();
94
        } elseif ($alert->getTitle()) {
95
            $data['title'] = $alert->getTitle();
96
        }
97
98
        if ($alert->getLocalizedAction()->getKey()) {
99
            $data['action-loc-key'] = $alert->getLocalizedAction()->getKey();
100
        }
101
102
        if ($alert->getLaunchImage()) {
103
            $data['launch-image'] = $alert->getLaunchImage();
104
        }
105
106
        return $data;
107
    }
108
}
109