RouteCollectionFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Http\Business\Route;
15
16
use Micro\Plugin\Http\Business\Locator\RouteLocatorFactoryInterface;
17
18
/**
19
 * @author Stanislau Komar <[email protected]>
20
 */
21
class RouteCollectionFactory implements RouteCollectionFactoryInterface
22
{
23
    private RouteCollectionInterface|null $routeCollection;
24
25
    public function __construct(
26
        private readonly RouteLocatorFactoryInterface $routeLocatorFactory
27
    ) {
28 6
        $this->routeCollection = null;
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function create(): RouteCollectionInterface
35
    {
36 6
        if ($this->routeCollection) {
37
            return $this->routeCollection;
38
        }
39
40 6
        $collection = new RouteCollection();
41
42 6
        $locator = $this->routeLocatorFactory->create();
43 6
        foreach ($locator->locate() as $route) {
44 6
            $collection->addRoute($route);
45
        }
46
47 6
        return $this->routeCollection = $collection;
48
    }
49
}
50