Completed
Push — master ( b2c31d...142309 )
by Vitaliy
01:49
created

Payload::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
 * Payload model
16
 */
17
class Payload
18
{
19
    /**
20
     * @var Aps
21
     */
22
    private $aps;
23
24
    /**
25
     * @var array
26
     */
27
    private $customData;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param Aps   $apsData
33
     * @param array $customData
34
     */
35
    public function __construct(Aps $apsData, array $customData = [])
36
    {
37
        $this->aps = $apsData;
38
        $this->customData = $customData;
39
    }
40
41
    /**
42
     * Set aps
43
     *
44
     * @param Aps $aps
45
     *
46
     * @return Payload
47
     */
48
    public function withAps(Aps $aps) : Payload
49
    {
50
        $cloned = clone $this;
51
52
        $cloned->aps = $aps;
53
54
        return $cloned;
55
    }
56
57
    /**
58
     * Get APS data
59
     *
60
     * @return Aps
61
     */
62
    public function getAps() : Aps
63
    {
64
        return $this->aps;
65
    }
66
67
    /**
68
     * Add or replace custom data
69
     *
70
     * @param string $name
71
     * @param mixed  $value
72
     *
73
     * @return Payload
74
     *
75
     * @throws \InvalidArgumentException
76
     */
77
    public function withCustomData(string $name, $value)
78
    {
79
        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...
80
            throw new \InvalidArgumentException(sprintf(
81
                'The custom data value should be a scalar or \JsonSerializable instance, but "%s" given.',
82
                is_object($value) ? get_class($value) : gettype($value)
83
            ));
84
        }
85
86
        $cloned = clone $this;
87
88
        $cloned->customData[$name] = $value;
89
90
        return $cloned;
91
    }
92
93
    /**
94
     * Get custom data
95
     *
96
     * @return array
97
     */
98
    public function getCustomData()
99
    {
100
        return $this->customData;
101
    }
102
}
103