ControllerCollection   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 24
ccs 6
cts 6
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 3 1
A getErrorMessage() 0 3 1
A __construct() 0 5 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Rest/ControllerCollection.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Rest;
10
11
use App\Collection\CollectionTrait;
12
use App\Rest\Interfaces\ControllerInterface;
13
use Closure;
14
use Countable;
15
use IteratorAggregate;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
18
use function sprintf;
19
20
/**
21
 * Class ControllerCollection
22
 *
23
 * @package App\Rest
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 *
26
 * @method ControllerInterface get(string $className)
27
 * @method IteratorAggregate<int, ControllerInterface> getAll()
28
 *
29
 * @template T<ControllerInterface>
30
 */
31
class ControllerCollection implements Countable
32
{
33
    use CollectionTrait;
34
35
    /**
36
     * Collection constructor.
37
     *
38
     * @phpstan-param IteratorAggregate<int, ControllerInterface> $items
39
     */
40 93
    public function __construct(
41
        #[TaggedIterator('app.rest.controller')]
42
        protected readonly IteratorAggregate $items,
43
        protected readonly LoggerInterface $logger,
44
    ) {
45 93
    }
46
47 1
    public function getErrorMessage(string $className): string
48
    {
49 1
        return sprintf('REST controller \'%s\' does not exist', $className);
50
    }
51
52 90
    public function filter(string $className): Closure
53
    {
54 90
        return static fn (ControllerInterface $restController): bool => $restController instanceof $className;
55
    }
56
}
57