Passed
Pull Request — master (#66)
by Vitaliy
03:59 queued 02:18
created

PayloadEncoder::convertApsToArray()   C

Complexity

Conditions 10
Paths 256

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 6.1066
c 0
b 0
f 0
cc 10
nc 256
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Apple\ApnPush\Model\Sound;
20
21
/**
22
 * The encoder for encode notification payload to string for next send to Apple Push Notification Service
23
 */
24
class PayloadEncoder implements PayloadEncoderInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function encode(Payload $payload): string
30
    {
31
        $data = [
32
            'aps' => $this->convertApsToArray($payload->getAps()),
33
        ];
34
35
        $data = \array_merge($payload->getCustomData(), $data);
36
37
        return \json_encode($data);
38
    }
39
40
    /**
41
     * Convert APS data to array
42
     *
43
     * @param Aps $aps
44
     *
45
     * @return array
46
     */
47
    private function convertApsToArray(Aps $aps): array
48
    {
49
        $data = [];
50
51
        if ($aps->getAlert()) {
52
            $data['alert'] = $this->convertAlertToArray($aps->getAlert());
53
        }
54
55
        if ($aps->getSound()) {
56
            $sound = $aps->getSound();
57
58
            if ($sound instanceof Sound) {
59
                $data['sound'] = [
60
                    'critical' => $sound->isCritical() ? 1 : 0,
61
                    'name'     => $sound->getName(),
62
                    'volume'   => $sound->getVolume(),
63
                ];
64
            } else {
65
                // Sound pass as string
66
                $data['sound'] = $sound;
67
            }
68
        }
69
70
        if ($aps->getBadge() !== null) {
71
            $data['badge'] = $aps->getBadge();
72
        }
73
74
        if ($aps->getCategory()) {
75
            $data['category'] = $aps->getCategory();
76
        }
77
78
        if ($aps->isContentAvailable()) {
79
            $data['content-available'] = 1;
80
        }
81
82
        if ($aps->isMutableContent()) {
83
            $data['mutable-content'] = 1;
84
        }
85
86
        if ($aps->getThreadId()) {
87
            $data['thread-id'] = $aps->getThreadId();
88
        }
89
90
        return $data;
91
    }
92
93
    /**
94
     * Convert alert object to array
95
     *
96
     * @param Alert $alert
97
     *
98
     * @return array
99
     */
100
    private function convertAlertToArray(Alert $alert): array
101
    {
102
        $data = [];
103
104
        if ($alert->getBodyLocalized()->getKey()) {
105
            $data['loc-key'] = $alert->getBodyLocalized()->getKey();
106
            $data['loc-args'] = $alert->getBodyLocalized()->getArgs();
107
        }
108
109
        if ($alert->getBody() || !$alert->getBodyLocalized()->getKey()) {
110
            $data['body'] = $alert->getBody();
111
        }
112
113
        if ($alert->getTitleLocalized()->getKey()) {
114
            $data['title-loc-key'] = $alert->getTitleLocalized()->getKey();
115
            $data['title-loc-args'] = $alert->getTitleLocalized()->getArgs();
116
        }
117
118
        if ($alert->getTitle()) {
119
            $data['title'] = $alert->getTitle();
120
        }
121
122
        if ($alert->getActionLocalized()->getKey()) {
123
            $data['action-loc-key'] = $alert->getActionLocalized()->getKey();
124
        }
125
126
        if ($alert->getLaunchImage()) {
127
            $data['launch-image'] = $alert->getLaunchImage();
128
        }
129
130
        return $data;
131
    }
132
}
133