Passed
Push — master ( 6534fd...eb8cff )
by Caen
12:05 queued 13s
created

PageCollection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 74
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A discoverPages() 0 19 5
A boot() 0 3 1
A discover() 0 6 1
A __construct() 0 3 1
A getPage() 0 3 1
A parsePagesFor() 0 10 2
A getPages() 0 4 2
A discoverPagesFor() 0 4 1
1
<?php
2
3
namespace Hyde\Framework\Foundation;
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\Foundation\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): self
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
    /**
71
     * @param  string<\Hyde\Framework\Contracts\PageContract>  $pageClass
72
     * @return \Illuminate\Support\Collection<\Hyde\Framework\Contracts\PageContract>
73
     */
74
    protected function parsePagesFor(string $pageClass): Collection
75
    {
76
        $collection = new Collection();
77
78
        /** @var PageContract $pageClass */
79
        foreach ($pageClass::files() as $basename) {
80
            $collection->push($pageClass::parse($basename));
81
        }
82
83
        return $collection;
84
    }
85
86
    protected function discover(PageContract $page): self
87
    {
88
        // Create a new route for the given page, and add it to the index.
89
        $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

89
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
90
91
        return $this;
92
    }
93
}
94