Completed
Push — master ( a0b70c...692665 )
by Jesse
04:31
created

Ignore::hasThe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\IdentityMap;
5
6
/**
7
 * Ignore a certain class, for instance a value object.
8
 *
9
 * @author Stratadox
10
 */
11
final class Ignore implements MapsObjectsByIdentity
12
{
13
    private $ignoredClass;
14
    private $identityMap;
15
16
    private function __construct(string $ignoredClass, MapsObjectsByIdentity $identityMap)
17
    {
18
        $this->ignoredClass = $ignoredClass;
19
        $this->identityMap = $identityMap->removeAllObjectsOfThe($ignoredClass);
20
    }
21
22
    /**
23
     * Wraps the identity map with a decorator that ignores a certain class.
24
     *
25
     * @param string                $ignoredClass The class to be ignored.
26
     * @param MapsObjectsByIdentity $identityMap  The identity map to wrap.
27
     * @return MapsObjectsByIdentity              The wrapped identity map.
28
     */
29
    public static function the(string $ignoredClass, MapsObjectsByIdentity $identityMap): MapsObjectsByIdentity
30
    {
31
        return new self($ignoredClass, $identityMap);
32
    }
33
34
    /**
35
     * Wraps a new identity map with decorators that ignore certain classes.
36
     *
37
     * @param string ...$ignoredClass The classes to be ignored.
38
     * @return MapsObjectsByIdentity  The wrapped identity map.
39
     */
40
    public static function these(string ...$ignoredClasses): MapsObjectsByIdentity
41
    {
42
        $map = IdentityMap::startEmpty();
43
        foreach ($ignoredClasses as $class) {
44
            $map = Ignore::the($class, $map);
45
        }
46
        return $map;
47
    }
48
49
    /** @inheritdoc */
50
    public function has(string $class, string $id): bool
51
    {
52
        return $this->identityMap->has($class, $id);
53
    }
54
55
    /** @inheritdoc */
56
    public function hasThe(object $object): bool
57
    {
58
        return $this->identityMap->hasThe($object);
59
    }
60
61
    /** @inheritdoc */
62
    public function get(string $class, string $id): object
63
    {
64
        return $this->identityMap->get($class, $id);
65
    }
66
67
    /** @inheritdoc */
68
    public function add(string $id, object $object): MapsObjectsByIdentity
69
    {
70
        if ($object instanceof $this->ignoredClass) {
71
            return $this;
72
        }
73
        return new self($this->ignoredClass, $this->identityMap->add($id, $object));
74
    }
75
76
    /** @inheritdoc */
77
    public function remove(string $class, string $id): MapsObjectsByIdentity
78
    {
79
        if ($class === $this->ignoredClass) {
80
            return $this;
81
        }
82
        return $this->newMap($this->identityMap->remove($class, $id));
83
    }
84
85
    /** @inheritdoc */
86
    public function removeThe(object $object): MapsObjectsByIdentity
87
    {
88
        return $this->newMap($this->identityMap->removeThe($object));
89
    }
90
91
    /** @inheritdoc */
92
    public function removeAllObjectsOfThe(string $class): MapsObjectsByIdentity
93
    {
94
        if ($class === $this->ignoredClass) {
95
            return $this;
96
        }
97
        return $this->newMap($this->identityMap->removeAllObjectsOfThe($class));
98
    }
99
100
    /** @inheritdoc */
101
    public function idOf(object $object): string
102
    {
103
        return $this->identityMap->idOf($object);
104
    }
105
106
    private function newMap(MapsObjectsByIdentity $map): MapsObjectsByIdentity
107
    {
108
        return new self($this->ignoredClass, $map);
109
    }
110
}
111