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

FilteredCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 91
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __get() 0 9 2
A set() 0 6 2
A filter() 0 17 3
A jsonSerialize() 0 11 3
A areAllArgumentsCollections() 0 4 2
A filterItems() 0 12 3
1
<?php
2
3
namespace Softonic\GraphQL\Mutation;
4
5
use Softonic\GraphQL\Mutation\Traits\MutationObjectHandler;
6
7
class FilteredCollection implements MutationObject, \JsonSerializable
8
{
9
    use MutationObjectHandler;
10
11
    /**
12
     * @var array<Item>
13
     */
14
    protected $arguments = [];
15
16
    /**
17
     * @var array
18
     */
19
    protected $config;
20
21
    /**
22
     * @var bool
23
     */
24
    private $hasChanged = false;
25
26 38
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
27
    {
28 38
        $this->arguments  = $arguments;
29 38
        $this->config     = $config;
30 38
        $this->hasChanged = $hasChanged;
31 38
    }
32
33 14
    public function __get(string $key): Collection
34
    {
35 14
        $items = [];
36 14
        foreach ($this->arguments as $argument) {
37 14
            $items[] = $argument->{$key};
38
        }
39
40 14
        return new Collection($items, $this->config[$key]->children);
41
    }
42
43 18
    public function set(array $data): void
44
    {
45 18
        foreach ($this->arguments as $argument) {
46 18
            $argument->set($data);
47
        }
48 18
    }
49
50 14
    public function filter(array $filters): FilteredCollection
51
    {
52 14
        $filteredData = [];
53 14
        if ($this->areAllArgumentsCollections()) {
54 4
            foreach ($this->arguments as $argument) {
55 4
                $filteredItems = $this->filterItems($argument->arguments, $filters);
0 ignored issues
show
Documentation introduced by
The property $arguments is declared private in Softonic\GraphQL\Mutation\Item. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
57 4
                $filteredData[] = new FilteredCollection($filteredItems, $this->config);
58
            }
59
        } else {
60 10
            $filteredItems = $this->filterItems($this->arguments, $filters);
61
62 10
            $filteredData = $filteredItems;
63
        }
64
65 14
        return new FilteredCollection($filteredData, $this->config);
66
    }
67
68 24
    public function jsonSerialize(): array
69
    {
70 24
        $items = [];
71 24
        foreach ($this->arguments as $item) {
72 24
            if ($item->hasChanged()) {
73 24
                $items[] = $item->jsonSerialize();
74
            }
75
        }
76
77 24
        return $items;
78
    }
79
80 14
    private function areAllArgumentsCollections(): bool
81
    {
82 14
        return (!empty($this->arguments[0]) && $this->arguments[0] instanceof Collection);
83
    }
84
85
    private function filterItems(array $arguments, array $filters): array
86
    {
87 14
        return array_filter($arguments, function ($item) use ($filters) {
88 14
            foreach ($filters as $filterKey => $filterValue) {
89 14
                if (!($item->{$filterKey} == $filterValue)) {
90 14
                    return false;
91
                }
92
            }
93
94 14
            return true;
95 14
        });
96
    }
97
}
98