Completed
Pull Request — master (#43)
by Jose Manuel
02:54
created

Item::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Softonic\GraphQL\DataObjects\Mutation;
4
5
use Softonic\GraphQL\Config\MutationTypeConfig;
6
use Softonic\GraphQL\DataObjects\AbstractItem;
7
use Softonic\GraphQL\DataObjects\Mutation\Traits\MutationObjectHandler;
8
9
class Item extends AbstractItem implements MutationObject
10
{
11
    use MutationObjectHandler;
12
13
    /**
14
     * @var array
15
     */
16
    protected $config;
17
18
    /**
19
     * @var bool
20
     */
21
    private $hasChanged = false;
22
23 100
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
24
    {
25 100
        parent::__construct($arguments);
26
27 100
        $this->config     = $config;
28 100
        $this->hasChanged = $hasChanged;
29 100
    }
30
31 72
    public function __get(string $key)
32
    {
33 72
        if ((!array_key_exists($key, $this->arguments) || ($this->arguments[$key] === null))
34 72
            && array_key_exists($key, $this->config)
35 72
            && ($this->config[$key]->type !== MutationTypeConfig::SCALAR_DATA_TYPE)
36
        ) {
37 8
            $mutationTypeClass = $this->config[$key]->type;
38
39 8
            $this->arguments[$key] = new $mutationTypeClass([], $this->config[$key]->children);
40
        }
41
42 72
        return parent::__get($key);
43
    }
44
45 32
    public function __set(string $key, $value): void
46
    {
47 32
        if (!array_key_exists($key, $this->arguments) || $this->arguments[$key] !== $value) {
48 30
            $this->hasChanged = true;
49
        }
50
51 32
        parent::__set($key, $value);
52 32
    }
53
54 6
    public function __unset(string $key): void
55
    {
56 6
        unset($this->arguments[$key]);
57
58 6
        $this->hasChanged = true;
59 6
    }
60
61 24
    public function set(array $data): void
62
    {
63 24
        foreach ($data as $key => $value) {
64 24
            $this->{$key} = $value;
65
        }
66 24
    }
67
68 56
    public function jsonSerialize(): array
69
    {
70 56
        if (!$this->hasChanged()) {
71 6
            return [];
72
        }
73
74 52
        return parent::jsonSerialize();
75
    }
76
}
77