ResourceRecorder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 35
c 3
b 0
f 0
dl 0
loc 115
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A resources() 0 3 1
A getResource() 0 9 2
A removeAllResource() 0 5 1
A __construct() 0 3 1
A hasResource() 0 3 1
A removeResource() 0 19 4
A addResource() 0 27 4
1
<?php
2
3
namespace Tleckie\Acl\Resource;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Class ResourceRecorder
9
 *
10
 * @package Tleckie\Acl\Resource
11
 * @author  Teodoro Leckie Westberg <[email protected]
12
 */
13
class ResourceRecorder implements ResourceRecorderInterface
14
{
15
    /** @var ResourceFactoryInterface */
16
    private ResourceFactoryInterface $resourceFactory;
17
18
    /** @var array */
19
    private array $resources = [];
20
21
    /**
22
     * ResourceRecorder constructor.
23
     *
24
     * @param ResourceFactoryInterface $resourceFactory
25
     */
26
    public function __construct(ResourceFactoryInterface $resourceFactory)
27
    {
28
        $this->resourceFactory = $resourceFactory;
29
    }
30
31
    /**
32
     * @param string|ResourceInterface $resource
33
     * @param array                    $parents
34
     * @return ResourceRecorderInterface
35
     */
36
    public function addResource(
37
        string|ResourceInterface $resource,
38
        array $parents = []
39
    ): ResourceRecorderInterface {
40
        if ($this->hasResource($resource)) {
41
            throw new InvalidArgumentException(
42
                sprintf('Resource [%s] already exists', $resource)
43
            );
44
        }
45
46
        $resource = $this->resourceFactory->create($resource);
47
48
        $this->resources[(string)$resource] = $resource;
49
50
        foreach ($parents as $parent) {
51
            if (!$this->hasResource($parent)) {
52
                $this->addResource($parent);
53
            }
54
55
            $parent = $this->getResource($parent);
56
57
            $parent->addChildren($resource);
58
59
            $resource->addParent($parent);
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function hasResource(ResourceInterface|string $resource): bool
69
    {
70
        return isset($this->resources[(string)$this->resourceFactory->create($resource)]);
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function getResource(ResourceInterface|string $resource): ResourceInterface
77
    {
78
        if (!$this->hasResource($resource)) {
79
            throw new InvalidArgumentException(
80
                sprintf('Resource [%s] not found', $resource)
81
            );
82
        }
83
84
        return $this->resources[(string)$resource];
85
    }
86
87
    /**
88
     * @param ResourceInterface|string $resource
89
     * @return ResourceRecorderInterface
90
     */
91
    public function removeResource(ResourceInterface|string $resource): ResourceRecorderInterface
92
    {
93
        try {
94
            $resource = $this->getResource($resource);
95
        } catch (InvalidArgumentException $exception) {
96
            throw new InvalidArgumentException(
97
                sprintf('Resource [%s] not found, can not remove', $resource),
98
                $exception->getCode(),
99
                $exception
100
            );
101
        }
102
103
        foreach ($this->resources as $item) {
104
            if ($resource === $item) {
105
                unset($this->resources[(string)$resource]);
106
            }
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return ResourceRecorderInterface
114
     */
115
    public function removeAllResource(): ResourceRecorderInterface
116
    {
117
        $this->resources = [];
118
119
        return $this;
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function resources(): array
126
    {
127
        return $this->resources;
128
    }
129
}
130