Completed
Pull Request — develop (#7)
by Romain
01:41
created

Data   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A build() 0 4 1
A fromArray() 0 13 3
1
<?php
2
namespace ker0x\Push\Adapter\Fcm\Message;
3
4
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidDataException;
5
6
class Data implements BuilderInterface
7
{
8
9
    /**
10
     * @var array
11
     */
12
    protected $data = [];
13
14
    /**
15
     * Data constructor.
16
     *
17
     * @param array|\ker0x\Push\Adapter\Fcm\Message\DataBuilder $dataBuilder The data we want to send.
18
     * @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidDataException
19
     */
20
    public function __construct($dataBuilder)
21
    {
22
        if (is_array($dataBuilder)) {
23
            $dataBuilder = $this->fromArray($dataBuilder);
24
        }
25
26
        $this->data = $dataBuilder->getAllData();
0 ignored issues
show
Documentation Bug introduced by
It seems like $dataBuilder->getAllData() can be null. However, the property $data is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

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

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
27
    }
28
29
    /**
30
     * Return data as an array.
31
     *
32
     * @return array
33
     */
34
    public function build()
35
    {
36
        return $this->data;
37
    }
38
39
    /**
40
     * Build data from an array.
41
     *
42
     * @param array $dataArray Array of data for the notification.
43
     * @return \ker0x\Push\Adapter\Fcm\Message\DataBuilder
44
     * @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidDataException
45
     */
46
    private function fromArray(array $dataArray)
47
    {
48
        if (empty($dataArray)) {
49
            throw InvalidDataException::arrayEmpty();
50
        }
51
52
        $dataBuilder = new DataBuilder();
53
        foreach ($dataArray as $key => $value) {
54
            $dataBuilder->addData($key, $value);
55
        }
56
57
        return $dataBuilder;
58
    }
59
}
60