Completed
Push — master ( fd9fb9...4b5f01 )
by Jesse
05:34
created

Ignore::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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
 * @deprecated Use Whitelist instead.
10
 * @see Whitelist
11
 *
12
 * @author Stratadox
13
 */
14
final class Ignore implements MapsObjectsByIdentity
15
{
16
    private $ignoredClass;
17
    private $identityMap;
18
19
    private function __construct(string $ignoredClass, MapsObjectsByIdentity $identityMap)
20
    {
21
        $this->ignoredClass = $ignoredClass;
22
        $this->identityMap = $identityMap->removeAllObjectsOfThe($ignoredClass);
23
    }
24
25
    /**
26
     * Wraps the identity map with a decorator that ignores a certain class.
27
     *
28
     * @param string                $ignoredClass The class to be ignored.
29
     * @param MapsObjectsByIdentity $identityMap  The identity map to wrap.
30
     * @return MapsObjectsByIdentity              The wrapped identity map.
31
     */
32
    public static function the(string $ignoredClass, MapsObjectsByIdentity $identityMap): MapsObjectsByIdentity
33
    {
34
        return new self($ignoredClass, $identityMap);
0 ignored issues
show
Deprecated Code introduced by
The class Stratadox\IdentityMap\Ignore has been deprecated with message: Use Whitelist instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
35
    }
36
37
    /**
38
     * Wraps a new identity map with decorators that ignore certain classes.
39
     *
40
     * @param string ...$ignoredClasses The classes to be ignored.
41
     * @return MapsObjectsByIdentity    The wrapped identity map.
42
     */
43
    public static function these(string ...$ignoredClasses): MapsObjectsByIdentity
44
    {
45
        $map = IdentityMap::startEmpty();
46
        foreach ($ignoredClasses as $class) {
47
            $map = Ignore::the($class, $map);
48
        }
49
        return $map;
50
    }
51
52
    /** @inheritdoc */
53
    public function has(string $class, string $id): bool
54
    {
55
        return $this->identityMap->has($class, $id);
56
    }
57
58
    /** @inheritdoc */
59
    public function hasThe(object $object): bool
60
    {
61
        return $this->identityMap->hasThe($object);
62
    }
63
64
    /** @inheritdoc */
65
    public function get(string $class, string $id): object
66
    {
67
        return $this->identityMap->get($class, $id);
68
    }
69
70
    /** @inheritdoc */
71
    public function add(string $id, object $object): MapsObjectsByIdentity
72
    {
73
        if ($object instanceof $this->ignoredClass) {
74
            return $this;
75
        }
76
        return new self($this->ignoredClass, $this->identityMap->add($id, $object));
0 ignored issues
show
Deprecated Code introduced by
The class Stratadox\IdentityMap\Ignore has been deprecated with message: Use Whitelist instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
77
    }
78
79
    /** @inheritdoc */
80
    public function remove(string $class, string $id): MapsObjectsByIdentity
81
    {
82
        if ($class === $this->ignoredClass) {
83
            return $this;
84
        }
85
        return $this->newMap($this->identityMap->remove($class, $id));
86
    }
87
88
    /** @inheritdoc */
89
    public function removeThe(object $object): MapsObjectsByIdentity
90
    {
91
        return $this->newMap($this->identityMap->removeThe($object));
92
    }
93
94
    /** @inheritdoc */
95
    public function removeAllObjectsOfThe(string $class): MapsObjectsByIdentity
96
    {
97
        if ($class === $this->ignoredClass) {
98
            return $this;
99
        }
100
        return $this->newMap($this->identityMap->removeAllObjectsOfThe($class));
101
    }
102
103
    /** @inheritdoc */
104
    public function idOf(object $object): string
105
    {
106
        return $this->identityMap->idOf($object);
107
    }
108
109
    /** @inheritdoc */
110
    public function classes(): array
111
    {
112
        return $this->identityMap->classes();
113
    }
114
115
    private function newMap(MapsObjectsByIdentity $map): MapsObjectsByIdentity
116
    {
117
        return new self($this->ignoredClass, $map);
0 ignored issues
show
Deprecated Code introduced by
The class Stratadox\IdentityMap\Ignore has been deprecated with message: Use Whitelist instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
118
    }
119
}
120