Passed
Push — master ( 32ab90...349214 )
by Caen
03:22 queued 14s
created

RouteCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
eloc 10
c 13
b 0
f 0
dl 0
loc 35
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoutes() 0 5 2
A runExtensionCallbacks() 0 4 2
A addRoute() 0 3 1
A runDiscovery() 0 4 1
A getRoute() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Kernel;
6
7
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8
use Hyde\Framework\Exceptions\RouteNotFoundException;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Models\Route;
11
12
/**
13
 * The RouteCollection contains all the routes, making it the Pseudo-Router for Hyde.
14
 *
15
 * @template T of \Hyde\Support\Models\Route
16
 * @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
17
 *
18
 * @property array<string, Route> $items The routes in the collection.
19
 *
20
 * This class is stored as a singleton in the HydeKernel.
21
 * You would commonly access it via the facade or Hyde helper:
22
 *
23
 * @see \Hyde\Foundation\Facades\Router
24
 * @see \Hyde\Hyde::routes()
25
 */
26
final class RouteCollection extends BaseFoundationCollection
27
{
28
    public function addRoute(Route $route): void
29
    {
30
        $this->put($route->getRouteKey(), $route);
0 ignored issues
show
Bug introduced by
$route->getRouteKey() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $this->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), $route);
Loading history...
31
    }
32
33
    protected function runDiscovery(): void
34
    {
35
        $this->kernel->pages()->each(function (HydePage $page): void {
36
            $this->addRoute(new Route($page));
37
        });
38
    }
39
40
    protected function runExtensionCallbacks(): void
41
    {
42
        foreach ($this->kernel->getExtensions() as $extension) {
43
            $extension->discoverRoutes($this);
44
        }
45
    }
46
47
    public function getRoute(string $routeKey): Route
48
    {
49
        return $this->get($routeKey) ?? throw new RouteNotFoundException(message: "Route [$routeKey] not found in route collection");
50
    }
51
52
    /**
53
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>|null  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>|null.
Loading history...
54
     * @return \Hyde\Foundation\Kernel\RouteCollection<string, \Hyde\Support\Models\Route>
55
     */
56
    public function getRoutes(?string $pageClass = null): RouteCollection
57
    {
58
        return $pageClass ? $this->filter(function (Route $route) use ($pageClass): bool {
59
            return $route->getPage() instanceof $pageClass;
60
        }) : $this;
61
    }
62
}
63