Passed
Push — master ( eb8cff...c3a046 )
by Caen
03:04 queued 14s
created

PageCollection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 64
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A discover() 0 6 1
A runDiscovery() 0 19 5
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 BaseSystemCollection
19
{
20
    public function getPage(string $sourcePath): PageContract
21
    {
22
        return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection');
23
    }
24
25
    public function getPages(?string $pageClass = null): self
26
    {
27
        return ! $pageClass ? $this : $this->filter(function (PageContract $page) use ($pageClass): bool {
28
            return $page instanceof $pageClass;
29
        });
30
    }
31
32
    protected function runDiscovery(): self
33
    {
34
        if (Features::hasBladePages()) {
35
            $this->discoverPagesFor(BladePage::class);
36
        }
37
38
        if (Features::hasMarkdownPages()) {
39
            $this->discoverPagesFor(MarkdownPage::class);
40
        }
41
42
        if (Features::hasBlogPosts()) {
43
            $this->discoverPagesFor(MarkdownPost::class);
44
        }
45
46
        if (Features::hasDocumentationPages()) {
47
            $this->discoverPagesFor(DocumentationPage::class);
48
        }
49
50
        return $this;
51
    }
52
53
    protected function discoverPagesFor(string $pageClass): void
54
    {
55
        $this->parsePagesFor($pageClass)->each(function ($page) {
56
            $this->discover($page);
57
        });
58
    }
59
60
    /**
61
     * @param  string<\Hyde\Framework\Contracts\PageContract>  $pageClass
62
     * @return \Illuminate\Support\Collection<\Hyde\Framework\Contracts\PageContract>
63
     */
64
    protected function parsePagesFor(string $pageClass): Collection
65
    {
66
        $collection = new Collection();
67
68
        /** @var PageContract $pageClass */
69
        foreach ($pageClass::files() as $basename) {
70
            $collection->push($pageClass::parse($basename));
71
        }
72
73
        return $collection;
74
    }
75
76
    protected function discover(PageContract $page): self
77
    {
78
        // Create a new route for the given page, and add it to the index.
79
        $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

79
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
80
81
        return $this;
82
    }
83
}
84