Payload   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createWithBody() 0 4 1
A withAps() 0 8 1
A getAps() 0 4 1
A withCustomData() 0 15 6
A getCustomData() 0 4 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->aps = $apsData;
40
        $this->customData = $customData;
41
    }
42
43
    /**
44
     * Create new payload with body only.
45
     *
46
     * @param string $body
47
     *
48
     * @return Payload
49
     */
50
    public static function createWithBody(string $body): Payload
51
    {
52
        return new self(new Aps(new Alert($body)));
53
    }
54
55
    /**
56
     * Set aps
57
     *
58
     * @param Aps $aps
59
     *
60
     * @return Payload
61
     */
62
    public function withAps(Aps $aps): Payload
63
    {
64
        $cloned = clone $this;
65
66
        $cloned->aps = $aps;
67
68
        return $cloned;
69
    }
70
71
    /**
72
     * Get APS data
73
     *
74
     * @return Aps
75
     */
76
    public function getAps(): Aps
77
    {
78
        return $this->aps;
79
    }
80
81
    /**
82
     * Add or replace custom data
83
     *
84
     * @param string $name
85
     * @param mixed  $value
86
     *
87
     * @return Payload
88
     *
89
     * @throws \InvalidArgumentException
90
     */
91
    public function withCustomData(string $name, $value): Payload
92
    {
93
        if ($value && !is_array($value) && !is_scalar($value) && !$value instanceof \JsonSerializable) {
94
            throw new \InvalidArgumentException(sprintf(
95
                'The custom data value should be a scalar or \JsonSerializable instance, but "%s" given.',
96
                is_object($value) ? get_class($value) : gettype($value)
97
            ));
98
        }
99
100
        $cloned = clone $this;
101
102
        $cloned->customData[$name] = $value;
103
104
        return $cloned;
105
    }
106
107
    /**
108
     * Get custom data
109
     *
110
     * @return array
111
     */
112
    public function getCustomData(): array
113
    {
114
        return $this->customData;
115
    }
116
}
117