Completed
Push — master ( 58620c...f23d9f )
by Vitaliy
02:20
created

Message::withCustomData()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 2
nop 2
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\Model;
13
14
/**
15
 * Push message
16
 */
17
class Message
18
{
19
    /**
20
     * @var ApnId
21
     */
22
    private $id;
23
24
    /**
25
     * @var ApsData
26
     */
27
    private $aps;
28
29
    /**
30
     * @var Priority
31
     */
32
    private $priority;
33
34
    /**
35
     * @var \DateTime
36
     */
37
    private $expiration;
38
39
    /**
40
     * @var array
41
     */
42
    private $customData;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param ApsData         $apsData
48
     * @param ApnId|null      $id
49
     * @param Priority|null   $priority
50
     * @param Expiration|null $expiration
51
     * @param array           $customData
52
     */
53
    public function __construct(
54
        ApsData $apsData,
55
        ApnId $id = null,
56
        Priority $priority = null,
57
        Expiration $expiration = null,
58
        array $customData = []
59
    ) {
60
        $this->aps = $apsData;
61
        $this->priority = $priority ?: Priority::fromNull();
62
        $this->id = $id ?: ApnId::fromNull();
63
        $this->expiration = $expiration ?: Expiration::fromNull();
0 ignored issues
show
Documentation Bug introduced by
It seems like $expiration ?: \Apple\Ap...\Expiration::fromNull() of type object<Apple\ApnPush\Model\Expiration> is incompatible with the declared type object<DateTime> of property $expiration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
64
        $this->customData = $customData;
65
    }
66
67
    /**
68
     * Get identifier of message
69
     *
70
     * @return ApnId
71
     */
72
    public function getId() : ApnId
73
    {
74
        return $this->id;
75
    }
76
77
    /**
78
     * Get APS data
79
     *
80
     * @return ApsData
81
     */
82
    public function getApsData() : ApsData
83
    {
84
        return $this->aps;
85
    }
86
87
    /**
88
     * Get priority
89
     *
90
     * @return Priority
91
     */
92
    public function getPriority() : Priority
93
    {
94
        return $this->priority;
95
    }
96
97
    /**
98
     * Get expiration
99
     *
100
     * @return Expiration
101
     */
102
    public function getExpiration() : Expiration
103
    {
104
        return $this->expiration;
105
    }
106
107
    /**
108
     * Add or replace custom data
109
     *
110
     * @param string $name
111
     * @param mixed  $value
112
     *
113
     * @return Message
114
     *
115
     * @throws \InvalidArgumentException
116
     */
117
    public function withCustomData(string $name, $value)
118
    {
119
        if ($value && !is_array($value) && !is_scalar($value) && !$value instanceof \JsonSerializable) {
1 ignored issue
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
120
            throw new \InvalidArgumentException(sprintf(
121
                'The custom data value should be a scalar or \JsonSerializable instance, but "%s" given.',
122
                is_object($value) ? get_class($value) : gettype($value)
123
            ));
124
        }
125
126
        $cloned = clone $this;
127
128
        $cloned->customData[$name] = $value;
129
130
        return $cloned;
131
    }
132
133
    /**
134
     * Get custom data
135
     *
136
     * @return array
137
     */
138
    public function getCustomData()
139
    {
140
        return $this->customData;
141
    }
142
}
143