ResourceRegistry::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 2
b 1
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Resource\Registry;
6
7
use LAG\AdminBundle\Exception\Exception;
8
use LAG\AdminBundle\Exception\UnexpectedTypeException;
9
use LAG\AdminBundle\Metadata\AdminResource;
10
use LAG\AdminBundle\Metadata\Factory\AdminFactoryInterface;
0 ignored issues
show
Bug introduced by
The type LAG\AdminBundle\Metadata...y\AdminFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use LAG\AdminBundle\Metadata\Locator\MetadataLocatorInterface;
12
13
class ResourceRegistry implements ResourceRegistryInterface
14
{
15
    /** @var AdminResource[] */
16
    private array $resources = [];
17
    private bool $loaded = false;
18
19
    public function __construct(
20
        /** @var array<int, string> $resourcePaths */
21
        private array $resourcePaths,
22
        private MetadataLocatorInterface $locator,
23
        private AdminFactoryInterface $adminFactory,
24
    ) {
25
    }
26
27
    public function load(): void
28
    {
29
        if ($this->loaded) {
30
            return;
31
        }
32
33
        foreach ($this->resourcePaths as $path) {
34
            $resources = $this->locator->locateCollection($path);
35
36
            foreach ($resources as $resource) {
37
                if (!$resource instanceof AdminResource) {
38
                    throw new UnexpectedTypeException($resource, AdminResource::class);
39
                }
40
41
                if (!$resource->getName()) {
42
                    throw new Exception('The admin resource has no name');
43
                }
44
                $this->resources[$resource->getName()] = $this->adminFactory->create($resource);
45
            }
46
        }
47
        $this->loaded = true;
48
    }
49
50
    public function has(string $resourceName): bool
51
    {
52
        return \array_key_exists($resourceName, $this->resources);
53
    }
54
55
    public function get(string $resourceName): AdminResource
56
    {
57
        $this->load();
58
59
        if (!$this->has($resourceName)) {
60
            throw new Exception('Resource with name "'.$resourceName.'" not found');
61
        }
62
63
        return $this->resources[$resourceName];
64
    }
65
66
    public function all(): array
67
    {
68
        $this->load();
69
70
        return $this->resources;
71
    }
72
73
    public function getResourceNames(): array
74
    {
75
        return array_keys($this->resources);
76
    }
77
}
78