ResourceIndex::locateResources()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 12
nc 6
nop 0
1
<?php
2
3
/*
4
 * This file is part of the "elao/api-resources-metadata" package.
5
 *
6
 * Copyright (C) 2016 Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\ApiResourcesMetadata\Resource;
12
13
use Elao\ApiResourcesMetadata\Exception\InvalidArgumentException;
14
use Elao\ApiResourcesMetadata\Resource\Locator\LocatorInterface;
15
use Psr\Cache\CacheItemPoolInterface;
16
17
class ResourceIndex
18
{
19
    /**
20
     * @internal
21
     */
22
    const CACHE_KEY = 'elao.api_resources_metadata.indexed_resources';
23
24
    /** @var LocatorInterface[] */
25
    private $resourceLocators;
26
27
    /** @var array */
28
    private $resourcesCache;
29
30
    /** @var CacheItemPoolInterface|null */
31
    private $cache;
32
33
    public function __construct(array $resourceLocators, CacheItemPoolInterface $cache = null)
34
    {
35
        $this->resourceLocators = $resourceLocators;
36
        $this->cache = $cache;
37
    }
38
39
    /**
40
     * @return array resources classes indexed by short name
41
     */
42
    public function all(): array
43
    {
44
        $this->locateResources();
45
46
        return $this->resourcesCache;
47
    }
48
49
    /**
50
     * @param string $resource Resource class or short name
51
     *
52
     * @return bool True if the class or short name identifies an existing resource
53
     */
54
    public function has(string $resource): bool
55
    {
56
        return isset($this->all()[$resource]) || in_array($resource, $this->all(), true);
57
    }
58
59
    /**
60
     * @param string $resourceClass The resource FQCN
61
     *
62
     * @throws InvalidArgumentException When no resource found
63
     *
64
     * @return string The resource short name
65
     */
66
    public function getShortName(string $resourceClass): string
67
    {
68
        if (!$this->has($resourceClass)) {
69
            throw new InvalidArgumentException(sprintf('Class "%s" is not indexed as a resource.', $resourceClass));
70
        }
71
72
        return array_search($resourceClass, $this->all(), true);
73
    }
74
75
    /**
76
     * @param string $shortName The resource short name
77
     *
78
     * @throws InvalidArgumentException When no resource found
79
     *
80
     * @return string The resource FQCN
81
     */
82
    public function getResourceClass(string $shortName): string
83
    {
84
        if (!$this->has($shortName)) {
85
            throw new InvalidArgumentException(sprintf('"%s" is not indexed as a resource.', $shortName));
86
        }
87
88
        return $this->all()[$shortName];
89
    }
90
91
    private function locateResources()
92
    {
93
        if (null !== $this->resourcesCache) {
94
            return $this->resourcesCache;
95
        }
96
97
        if (null !== $this->cache && $this->cache->getItem(self::CACHE_KEY)->isHit()) {
98
            return $this->resourcesCache = $this->cache->getItem(self::CACHE_KEY)->get();
99
        }
100
101
        $this->resourcesCache = [];
102
103
        foreach ($this->resourceLocators as $locator) {
104
            $this->resourcesCache += $locator->locate();
105
        }
106
107
        if (null !== $this->cache) {
108
            $item = $this->cache->getItem(self::CACHE_KEY);
109
            $item->set($this->resourcesCache);
110
            $this->cache->save($item);
111
        }
112
    }
113
}
114