Completed
Pull Request — master (#45)
by Jose Manuel
02:24
created

FilteredCollection::remove()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

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