Passed
Push — master ( d3f7f1...90b48b )
by Caen
03:27 queued 12s
created

RouteCollection::getRoutesForModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Hyde\Framework;
4
5
use Hyde\Framework\Contracts\HydeKernelContract;
6
use Hyde\Framework\Contracts\PageContract;
7
use Hyde\Framework\Contracts\RouteContract;
8
use Hyde\Framework\Models\Route;
9
use Illuminate\Support\Collection;
10
11
/**
12
 * @see \Hyde\Framework\PageCollection
13
 * @see \Hyde\Framework\Testing\Feature\RouteTest
14
 */
15
final class RouteCollection extends Collection
16
{
17
    protected HydeKernelContract $kernel;
18
19
    public static function boot(HydeKernelContract $kernel): self
20
    {
21
        return (new self())->setKernel($kernel)->discoverRoutes();
22
    }
23
24
    protected function __construct($items = [])
25
    {
26
        parent::__construct($items);
27
    }
28
29
    protected function setKernel(HydeKernelContract $kernel): self
30
    {
31
        $this->kernel = $kernel;
32
33
        return $this;
34
    }
35
36
    public function getRoutes(): self
37
    {
38
        return $this;
39
    }
40
41
    /**
42
     * @deprecated Will be merged into getRoutes()
43
     */
44
    public function getRoutesForModel(string $pageClass): self
45
    {
46
        // Return a new filtered collection with only routes that are for the given page class.
47
        return $this->filter(function (RouteContract $route) use ($pageClass) {
48
            return $route->getSourceModel() instanceof $pageClass;
49
        });
50
    }
51
52
    /**
53
     * This internal method adds the specified route to the route index.
54
     * It's made public so package developers can hook into the routing system.
55
     */
56
    public function addRoute(RouteContract $route): self
57
    {
58
        $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

58
        $this->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), $route);
Loading history...
59
60
        return $this;
61
    }
62
63
    protected function discover(PageContract $page): self
64
    {
65
        // Create a new route for the given page, and add it to the index.
66
        $this->addRoute(new Route($page));
67
68
        return $this;
69
    }
70
71
    protected function discoverRoutes(): self
72
    {
73
        $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

73
        $this->kernel->/** @scrutinizer ignore-call */ 
74
                       pages()->each(function (PageContract $page) {
Loading history...
74
            $this->discover($page);
75
        });
76
77
        return $this;
78
    }
79
}
80