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

PageCollection::parsePagesFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Hyde\Framework;
4
5
use Hyde\Framework\Contracts\PageContract;
6
use Hyde\Framework\Exceptions\FileNotFoundException;
7
use Hyde\Framework\Helpers\Features;
8
use Hyde\Framework\Models\Pages\BladePage;
9
use Hyde\Framework\Models\Pages\DocumentationPage;
10
use Hyde\Framework\Models\Pages\MarkdownPage;
11
use Hyde\Framework\Models\Pages\MarkdownPost;
12
use Illuminate\Support\Collection;
13
14
/**
15
 * @see \Hyde\Framework\RouteCollection
16
 * @see \Hyde\Framework\Testing\Feature\PageCollectionTest
17
 */
18
final class PageCollection extends Collection
19
{
20
    public static function boot(): self
21
    {
22
        return (new self())->discoverPages();
23
    }
24
25
    protected function __construct($items = [])
26
    {
27
        parent::__construct($items);
28
    }
29
30
    public function getPage(string $sourcePath): PageContract
31
    {
32
        return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection');
33
    }
34
35
    public function getPages(?string $pageClass = null): Collection
36
    {
37
        return ! $pageClass ? $this : $this->filter(function (PageContract $page) use ($pageClass): bool {
38
            return $page instanceof $pageClass;
39
        });
40
    }
41
42
    protected function discoverPages(): self
43
    {
44
        if (Features::hasBladePages()) {
45
            $this->discoverPagesFor(BladePage::class);
46
        }
47
48
        if (Features::hasMarkdownPages()) {
49
            $this->discoverPagesFor(MarkdownPage::class);
50
        }
51
52
        if (Features::hasBlogPosts()) {
53
            $this->discoverPagesFor(MarkdownPost::class);
54
        }
55
56
        if (Features::hasDocumentationPages()) {
57
            $this->discoverPagesFor(DocumentationPage::class);
58
        }
59
60
        return $this;
61
    }
62
63
    protected function discoverPagesFor(string $pageClass): void
64
    {
65
        $this->parsePagesFor($pageClass)->each(function ($page) {
66
            $this->discover($page);
67
        });
68
    }
69
70
    protected function parsePagesFor(string $pageClass): Collection
71
    {
72
        $collection = new Collection();
73
74
        /** @var PageContract $pageClass */
75
        foreach ($pageClass::files() as $basename) {
76
            $collection->push($pageClass::parse($basename));
77
        }
78
79
        return $collection;
80
    }
81
82
    protected function discover(PageContract $page): self
83
    {
84
        // Create a new route for the given page, and add it to the index.
85
        $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

85
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
86
87
        return $this;
88
    }
89
}
90