CollectionProxy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 18
dl 0
loc 59
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Collections;
6
7
use CCT\Component\Collections\Interactors\ArrayAggregator;
8
use CCT\Component\Collections\Interactors\ArrayInspector;
9
use CCT\Component\Collections\Interactors\ArraySegregator;
10
use CCT\Component\Collections\Interactors\ArraySorter;
11
use CCT\Component\Collections\Interactors\InteractorInterface;
12
13
class CollectionProxy
14
{
15
    /**
16
     * @var CollectionInterface
17
     */
18
    protected $collection;
19
20
    /**
21
     * @var object
22
     */
23
    protected $proxy;
24
25
    /**
26
     * @var array
27
     */
28
    public static $proxies = [
29
        'sorter' => ArraySorter::class,
30
        'inspector' => ArrayInspector::class,
31
        'aggregator' => ArrayAggregator::class,
32
        'segregator' => ArraySegregator::class,
33
    ];
34
35
    /**
36
     * ArrayProxy constructor.
37
     *
38
     * @param CollectionInterface   $collection
39
     * @param string                $proxy
40
     */
41 15
    public function __construct(CollectionInterface $collection, string $proxy)
42
    {
43 15
        $this->collection = $collection;
44 15
        $this->proxy = new static::$proxies[$proxy]($collection->all());
45 15
    }
46
47
    /**
48
     * Proxy a method call onto the specified Interactor.
49
     *
50
     * @param string $method
51
     * @param array $parameters
52
     *
53
     * @return mixed
54
     */
55 15
    public function __call($method, $parameters)
56
    {
57 15
        $result = $this->proxy->{$method}(...$parameters);
58
59 13
        if (is_array($result)) {
60 9
            $this->collection->override($result);
0 ignored issues
show
Bug introduced by
The method override() does not exist on CCT\Component\Collections\CollectionInterface. It seems like you code against a sub-type of CCT\Component\Collections\CollectionInterface such as CCT\Component\Collections\ArrayCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            $this->collection->/** @scrutinizer ignore-call */ 
61
                               override($result);
Loading history...
61
62 9
            return $this->collection;
63
        }
64
65 4
        if ($result instanceof InteractorInterface || null === $result) {
66 1
            $this->collection->override($this->proxy->all());
67
68 1
            return $this->collection;
69
        }
70
71 3
        return $result;
72
    }
73
}
74