Passed
Push — master ( 25efb9...c70e91 )
by Caen
04:09 queued 12s
created

PageCollection::getPages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\FileNotFoundException;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Filesystem\SourceFile;
11
12
/**
13
 * The PageCollection contains all the instantiated pages.
14
 *
15
 * @template T of \Hyde\Pages\Concerns\HydePage
16
 * @template-extends \Hyde\Foundation\Concerns\BaseFoundationCollection<string, T>
17
 *
18
 * @property array<string, HydePage> $items The pages 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\PageCollection
24
 * @see \Hyde\Hyde::pages()
25
 */
26
final class PageCollection extends BaseFoundationCollection
27
{
28
    public function addPage(HydePage $page): void
29
    {
30
        $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

30
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
31
    }
32
33
    protected function runDiscovery(): void
34
    {
35
        $this->kernel->files()->each(function (SourceFile $file): void {
36
            $this->addPage($this->parsePage($file->pageClass, $file->getPath()));
37
        });
38
    }
39
40
    protected function runExtensionCallbacks(): void
41
    {
42
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
43
        foreach ($this->kernel->getExtensions() as $extension) {
44
            $extension->discoverPages($this);
45
        }
46
    }
47
48
    /** @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
49
    protected static function parsePage(string $pageClass, string $path)
50
    {
51
        return $pageClass::parse($pageClass::pathToIdentifier($path));
52
    }
53
54
    public function getPage(string $sourcePath): HydePage
55
    {
56
        return $this->get($sourcePath) ?? throw new FileNotFoundException($sourcePath);
57
    }
58
59
    /**
60
     * @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...
61
     * @return \Hyde\Foundation\Kernel\PageCollection<string, \Hyde\Pages\Concerns\HydePage>
62
     */
63
    public function getPages(?string $pageClass = null): PageCollection
64
    {
65
        return $pageClass ? $this->filter(function (HydePage $page) use ($pageClass): bool {
66
            return $page instanceof $pageClass;
67
        }) : $this;
68
    }
69
}
70