Passed
Push — master ( f8c79c...1ed579 )
by Caen
03:55 queued 13s
created

PageCollection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 72
rs 10
c 4
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPage() 0 3 1
A getPages() 0 4 2
A discoverPagesFor() 0 4 1
A discover() 0 6 1
B runDiscovery() 0 27 7
A parsePagesFor() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation;
6
7
use Hyde\Facades\Features;
8
use Hyde\Foundation\Concerns\BaseFoundationCollection;
9
use Hyde\Framework\Exceptions\FileNotFoundException;
10
use Hyde\Pages\BladePage;
11
use Hyde\Pages\Concerns\HydePage;
12
use Hyde\Pages\DocumentationPage;
13
use Hyde\Pages\HtmlPage;
14
use Hyde\Pages\MarkdownPage;
15
use Hyde\Pages\MarkdownPost;
16
use Illuminate\Support\Collection;
17
18
/**
19
 * The PageCollection contains all the instantiated pages.
20
 *
21
 * This class is stored as a singleton in the HydeKernel.
22
 * You would commonly access it via one of the facades:
23
 *
24
 * @todo We could improve this by catching exceptions and rethrowing them using a
25
 *       DiscoveryException to make it clear that the problem is with the discovery process.
26
 *
27
 * @see \Hyde\Foundation\Facades\PageCollection
28
 * @see \Hyde\Hyde::pages()
29
 */
30
final class PageCollection extends BaseFoundationCollection
31
{
32
    public function getPage(string $sourcePath): HydePage
33
    {
34
        return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection');
35
    }
36
37
    public function getPages(?string $pageClass = null): self
38
    {
39
        return ! $pageClass ? $this : $this->filter(function (HydePage $page) use ($pageClass): bool {
40
            return $page instanceof $pageClass;
41
        });
42
    }
43
44
    protected function runDiscovery(): self
45
    {
46
        if (Features::hasHtmlPages()) {
47
            $this->discoverPagesFor(HtmlPage::class);
48
        }
49
50
        if (Features::hasBladePages()) {
51
            $this->discoverPagesFor(BladePage::class);
52
        }
53
54
        if (Features::hasMarkdownPages()) {
55
            $this->discoverPagesFor(MarkdownPage::class);
56
        }
57
58
        if (Features::hasMarkdownPosts()) {
59
            $this->discoverPagesFor(MarkdownPost::class);
60
        }
61
62
        if (Features::hasDocumentationPages()) {
63
            $this->discoverPagesFor(DocumentationPage::class);
64
        }
65
66
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
67
            $this->discoverPagesFor($pageClass);
68
        }
69
70
        return $this;
71
    }
72
73
    protected function discoverPagesFor(string $pageClass): void
74
    {
75
        $this->parsePagesFor($pageClass)->each(function ($page): void {
76
            $this->discover($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
    protected function discover(HydePage $page): self
97
    {
98
        // Create a new route for the given page, and add it to the index.
99
        $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

99
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
100
101
        return $this;
102
    }
103
}
104