Passed
Push — master ( 0503bd...6a1d06 )
by Caen
03:16 queued 13s
created

PageCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parsePagesFor() 0 10 2
A addPage() 0 5 1
A discoverPagesFor() 0 4 1
A getPage() 0 3 1
A getPages() 0 4 2
A runDiscovery() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation;
6
7
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8
use Hyde\Framework\Exceptions\FileNotFoundException;
9
use Hyde\Pages\Concerns\HydePage;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * The PageCollection contains all the instantiated pages.
14
 *
15
 * This class is stored as a singleton in the HydeKernel.
16
 * You would commonly access it via one of the facades:
17
 *
18
 * @todo We could improve this by catching exceptions and rethrowing them using a
19
 *       DiscoveryException to make it clear that the problem is with the discovery process.
20
 *
21
 * @see \Hyde\Foundation\Facades\PageCollection
22
 * @see \Hyde\Hyde::pages()
23
 */
24
final class PageCollection extends BaseFoundationCollection
25
{
26
    public function getPage(string $sourcePath): HydePage
27
    {
28
        return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection');
29
    }
30
31
    public function getPages(?string $pageClass = null): self
32
    {
33
        return ! $pageClass ? $this : $this->filter(function (HydePage $page) use ($pageClass): bool {
34
            return $page instanceof $pageClass;
35
        });
36
    }
37
38
    /**
39
     * This method adds the specified page to the page collection.
40
     * It can be used by package developers to add a page that will be compiled.
41
     *
42
     * Note that this method when used outside of this class is only intended to be used for adding on-off pages;
43
     * If you are registering multiple pages, you may instead want to register an entire custom page class,
44
     * as that will allow you to utilize the full power of the HydePHP autodiscovery.
45
     *
46
     * When using this method, take notice of the following things:
47
     * 1. Be sure to register the page before the HydeKernel boots,
48
     *    otherwise it might not be fully processed by Hyde.
49
     * 2. Note that all pages will have their routes added to the route index,
50
     *    and subsequently be compiled during the build process.
51
     */
52
    public function addPage(HydePage $page): self
53
    {
54
        $this->put($page->getSourcePath(), $page);
0 ignored issues
show
Bug introduced by
$page->getSourcePath() 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

54
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
55
56
        return $this;
57
    }
58
59
    protected function runDiscovery(): self
60
    {
61
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
62
            $this->discoverPagesFor($pageClass);
63
        }
64
65
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
66
        foreach ($this->kernel->getRegisteredExtensions() as $extension) {
67
            $extension::discoverPages($this);
68
        }
69
70
        return $this;
71
    }
72
73
    protected function discoverPagesFor(string $pageClass): void
74
    {
75
        $this->parsePagesFor($pageClass)->each(function (HydePage $page): void {
76
            $this->addPage($page);
77
        });
78
    }
79
80
    /**
81
     * @param  string<\Hyde\Pages\Concerns\HydePage>  $pageClass
82
     * @return \Illuminate\Support\Collection<\Hyde\Pages\Concerns\HydePage>
83
     */
84
    protected function parsePagesFor(string $pageClass): Collection
85
    {
86
        $collection = new Collection();
87
88
        /** @var HydePage $pageClass */
89
        foreach ($pageClass::files() as $basename) {
90
            $collection->push($pageClass::parse($basename));
91
        }
92
93
        return $collection;
94
    }
95
}
96