Completed
Pull Request — master (#29)
by
unknown
06:43
created

Item::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
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\Mutation;
4
5
use Softonic\GraphQL\Traits\JsonSerializer;
6
7
class Item implements MutationObject, \JsonSerializable
8
{
9
    use JsonSerializer;
10
11
    private $arguments;
12
13
    private $config;
14
15
    private $hasChanged = false;
16
17 48
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
18
    {
19 48
        $this->arguments  = $arguments;
20 48
        $this->config     = $config;
21 48
        $this->hasChanged = $hasChanged;
22 48
    }
23
24 28
    public function __get(string $key)
25
    {
26 28
        if (is_null($this->arguments[$key]) && array_key_exists($key, $this->config)) {
27
            $mutationType = $this->config[$key]->type;
28
29
            $this->arguments[$key] = new $mutationType([], $this->config[$key]->children);
30
        }
31
32 28
        return $this->arguments[$key];
33
    }
34
35 26
    public function __set(string $key, $value)
36
    {
37 26
        $this->arguments[$key] = $value;
38
39 26
        $this->hasChanged = true;
40 26
    }
41
42 34
    public function hasChanged(): bool
43
    {
44 34
        foreach ($this->arguments as $argument) {
45 34
            if ($argument instanceof MutationObject && $argument->hasChanged()) {
46 26
                return true;
47
            }
48
        }
49
50 34
        return $this->hasChanged;
51
    }
52
}
53