Passed
Push — master ( eb8cff...c3a046 )
by Caen
03:04 queued 14s
created

RouteCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoutes() 0 4 2
A addRoute() 0 5 1
A discover() 0 6 1
A runDiscovery() 0 7 1
1
<?php
2
3
namespace Hyde\Framework\Foundation;
4
5
use Hyde\Framework\Contracts\PageContract;
6
use Hyde\Framework\Contracts\RouteContract;
7
use Hyde\Framework\Models\Route;
8
9
/**
10
 * Pseudo-Router for Hyde.
11
 *
12
 * @see \Hyde\Framework\Foundation\PageCollection
13
 * @see \Hyde\Framework\Testing\Feature\RouteTest
14
 * @see \Hyde\Framework\Testing\Feature\RouteCollectionTest
15
 *
16
 * This is not a router in the traditional sense that it decides where to go.
17
 * Instead, it creates a pre-generated object encapsulating the Hyde autodiscovery.
18
 *
19
 * This not only let us emulate Laravel route helpers, but also serve as the
20
 * canonical source of truth for the vital HydePHP autodiscovery process.
21
 *
22
 * The routes defined can then also be used to power the RealtimeCompiler without
23
 * having to reverse-engineer the source file mapping.
24
 *
25
 * Routes are not intended to be added manually, instead the route index is created using
26
 * the exact same rules as the current autodiscovery process and compiled file output.
27
 *
28
 * The route index serves as a multidimensional mapping allowing you to
29
 * determine where a source file will be compiled to, and where a compiled
30
 * file was generated from.
31
 */
32
final class RouteCollection extends BaseSystemCollection
33
{
34
    public function getRoutes(?string $pageClass = null): self
35
    {
36
        return ! $pageClass ? $this : $this->filter(function (RouteContract $route) use ($pageClass) {
37
            return $route->getSourceModel() instanceof $pageClass;
38
        });
39
    }
40
41
    /**
42
     * This internal method adds the specified route to the route index.
43
     * It's made public so package developers can hook into the routing system.
44
     */
45
    public function addRoute(RouteContract $route): self
46
    {
47
        $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

47
        $this->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), $route);
Loading history...
48
49
        return $this;
50
    }
51
52
    protected function discover(PageContract $page): self
53
    {
54
        // Create a new route for the given page, and add it to the index.
55
        $this->addRoute(new Route($page));
56
57
        return $this;
58
    }
59
60
    protected function runDiscovery(): self
61
    {
62
        $this->kernel->pages()->each(function (PageContract $page) {
0 ignored issues
show
Bug introduced by
The method pages() does not exist on Hyde\Framework\Contracts\HydeKernelContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\HydeKernelContract. ( Ignorable by Annotation )

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

62
        $this->kernel->/** @scrutinizer ignore-call */ 
63
                       pages()->each(function (PageContract $page) {
Loading history...
63
            $this->discover($page);
64
        });
65
66
        return $this;
67
    }
68
}
69