PayloadEncoder::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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