LocatorCached::lookup()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 2
b 0
f 0
1
<?php
2
3
/*
4
 *  This file is part of the Micro framework package.
5
 *
6
 *  (c) Stanislau Komar <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace Micro\Plugin\Locator\Locator;
13
14
/**
15
 * @todo: Update this afrer implements micro/cache plugin.
16
 */
17
class LocatorCached implements LocatorInterface
18
{
19
    /**
20
     * @var array<string, array<class-string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, array<class-string>> at position 6 could not be parsed: Unknown type name 'class-string' at position 6 in array<string, array<class-string>>.
Loading history...
21
     */
22
    private array $results;
23
24 1
    public function __construct(
25
        private readonly LocatorInterface $locator
26
    ) {
27 1
        $this->results = [];
28
    }
29
30 1
    public function lookup(string $classOrInterfaceName): \Generator
31
    {
32 1
        if (\array_key_exists($classOrInterfaceName, $this->results)) {
33 1
            foreach ($this->results[$classOrInterfaceName] as $class) {
34 1
                yield $class;
35
            }
36
37 1
            return;
38
        }
39
40 1
        $results = [];
41 1
        foreach ($this->locator->lookup($classOrInterfaceName) as $class) {
42 1
            $results[] = $class;
43
44 1
            yield $class;
45
        }
46
47 1
        $this->results[$classOrInterfaceName] = $results;
48
    }
49
}
50