Passed
Push — master ( 60a2b8...a4571b )
by Chris
07:43
created

CollectionKernelSubsystemFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 30
dl 0
loc 56
ccs 27
cts 27
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPropertyResolver() 0 3 1
A __construct() 0 11 2
A getArrayDriver() 0 19 4
A getCollectionComparator() 0 5 2
1
<?php
2
3
namespace WebTheory\Collection\Kernel\Factory;
4
5
use WebTheory\Collection\Comparison\PropertyBasedCollectionComparator;
6
use WebTheory\Collection\Comparison\PropertyBasedObjectComparator;
7
use WebTheory\Collection\Comparison\RuntimeIdBasedCollectionComparator;
8
use WebTheory\Collection\Comparison\RuntimeIdBasedObjectComparator;
9
use WebTheory\Collection\Contracts\ArrayDriverInterface;
10
use WebTheory\Collection\Contracts\CollectionComparatorInterface;
11
use WebTheory\Collection\Contracts\ObjectComparatorInterface;
12
use WebTheory\Collection\Contracts\PropertyResolverInterface;
13
use WebTheory\Collection\Driver\AutoKeyedMap;
14
use WebTheory\Collection\Driver\IdentifiableItemList;
15
use WebTheory\Collection\Driver\StandardList;
16
use WebTheory\Collection\Resolution\PropertyResolver;
17
18
class CollectionKernelSubsystemFactory
19
{
20
    protected ?string $identifier;
21
22
    protected array $accessors;
23
24
    protected bool $mapToIdentifier;
25
26
    protected PropertyResolverInterface $propertyResolver;
27
28
    protected ObjectComparatorInterface $objectComparator;
29
30 216
    public function __construct(?string $identifier, array $accessors, bool $mapToIdentifier)
31
    {
32 216
        $this->identifier = $identifier;
33 216
        $this->accessors = $accessors;
34 216
        $this->mapToIdentifier = $mapToIdentifier;
35
36 216
        $this->propertyResolver = new PropertyResolver($accessors);
37
38 216
        $this->objectComparator = $identifier
39 216
            ? new PropertyBasedObjectComparator($this->propertyResolver, $identifier)
40 75
            : new RuntimeIdBasedObjectComparator();
41
    }
42
43 216
    public function getPropertyResolver(): PropertyResolverInterface
44
    {
45 216
        return $this->propertyResolver;
46
    }
47
48 216
    public function getCollectionComparator(): CollectionComparatorInterface
49
    {
50 216
        return $this->identifier
51 216
            ? new PropertyBasedCollectionComparator($this->propertyResolver, $this->identifier)
52 216
            : new RuntimeIdBasedCollectionComparator();
53
    }
54
55 216
    public function getArrayDriver(): ArrayDriverInterface
56
    {
57 216
        if ($this->identifier && $this->mapToIdentifier) {
58 33
            $driver = new AutoKeyedMap(
59 33
                $this->identifier,
60 33
                $this->propertyResolver,
61 33
                $this->objectComparator
62
            );
63 216
        } elseif ($this->identifier) {
64 216
            $driver = new IdentifiableItemList(
65 216
                $this->identifier,
66 216
                $this->propertyResolver,
67 216
                $this->objectComparator
68
            );
69
        } else {
70 75
            $driver = new StandardList($this->objectComparator);
71
        }
72
73 216
        return $driver;
74
    }
75
}
76