Passed
Push — morloderex-morloderex/master ( 901a7b )
by Vitaliy
02:11
created

Payload::createWithBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Model;
15
16
/**
17
 * Payload model
18
 */
19
class Payload
20
{
21
    /**
22
     * @var Aps
23
     */
24
    private $aps;
25
26
    /**
27
     * @var array
28
     */
29
    private $customData;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param Aps   $apsData
35
     * @param array $customData
36
     */
37
    public function __construct(Aps $apsData, array $customData = [])
38
    {
39
        $this->guardCustomData($customData);
40
41
        $this->aps = $apsData;
42
        $this->customData = $customData;
43
    }
44
45
    /**
46
     * Create new payload with body only.
47
     *
48
     * @param string $body
49
     *
50
     * @return Payload
51
     */
52
    public static function createWithBody(string $body): Payload
53
    {
54
        return new self(new Aps(new Alert($body)));
55
    }
56
57
    /**
58
     * Set aps
59
     *
60
     * @param Aps $aps
61
     *
62
     * @return Payload
63
     */
64
    public function withAps(Aps $aps): Payload
65
    {
66
        $cloned = clone $this;
67
68
        $cloned->aps = $aps;
69
70
        return $cloned;
71
    }
72
73
    /**
74
     * Get APS data
75
     *
76
     * @return Aps
77
     */
78
    public function getAps(): Aps
79
    {
80
        return $this->aps;
81
    }
82
83
    /**
84
     * Add or replace custom data
85
     *
86
     * @param string $name
87
     * @param mixed  $value
88
     *
89
     * @return Payload
90
     *
91
     * @throws \InvalidArgumentException
92
     */
93
    public function withCustomData(string $name, $value): Payload
94
    {
95
        $this->guardValue($value);
96
97
        $cloned = clone $this;
98
99
        $cloned->customData[$name] = $value;
100
101
        return $cloned;
102
    }
103
104
    /**
105
     * Get custom data
106
     *
107
     * @return array
108
     */
109
    public function getCustomData(): array
110
    {
111
        return $this->customData;
112
    }
113
114
    /**
115
     * Guard the custom data upon instantiation.
116
     *
117
     * @param array $data
118
     */
119
    private function guardCustomData(array $data)
120
    {
121
        if (false === empty($data)) {
122
            foreach ($data as $key => $value) {
123
                $this->guardValue($value);
124
                $this->guardKey($key);
125
            }
126
        }
127
    }
128
129
    private function guardValue($value)
130
    {
131
        if ($value && !is_array($value) && !is_scalar($value) && !$value instanceof \JsonSerializable) {
132
            throw new \InvalidArgumentException(sprintf(
133
                'The custom data value should be a scalar or \JsonSerializable instance, but "%s" given.',
134
                is_object($value) ? get_class($value) : gettype($value)
135
            ));
136
        }
137
    }
138
139
    private function guardKey($key)
140
    {
141
        if (! is_string($key)) {
142
            throw new \InvalidArgumentException(sprintf(
143
                'The custom data key should be a string, but "%s" given.',
144
                gettype($key)
145
            ));
146
        }
147
    }
148
}
149