Passed
Push — master ( 9b4e67...25f483 )
by Caen
02:57 queued 12s
created

PageModelConstructor::findTitleFromMarkdownHeadings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Contracts\AbstractMarkdownPage;
6
use Hyde\Framework\Contracts\AbstractPage;
7
use Hyde\Framework\Models\Pages\BladePage;
8
use Hyde\Framework\Models\Pages\DocumentationPage;
9
use Illuminate\Support\Str;
10
11
/**
12
 * Dynamically constructs data for a page model.
13
 *
14
 * @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest
15
 */
16
class PageModelConstructor
17
{
18
    /**
19
     * @var AbstractPage|AbstractMarkdownPage|BladePage
20
     */
21
    protected AbstractPage|AbstractMarkdownPage|BladePage $page;
22
23
    public static function run(AbstractPage $page): AbstractPage
24
    {
25
        return (new static($page))->get();
26
    }
27
28
    protected function __construct(AbstractPage $page)
29
    {
30
        $this->page = $page;
31
        $this->constructDynamicData();
32
    }
33
34
    protected function constructDynamicData(): void
35
    {
36
        // @deprecated v0.58.x-beta (will be added to docpage schema)
37
        if ($this->page instanceof DocumentationPage) {
38
            $this->page->category = static::getDocumentationPageCategory();
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\Framework\Actions\P...mentationPageCategory() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            /** @scrutinizer ignore-call */ 
39
            $this->page->category = static::getDocumentationPageCategory();
Loading history...
Bug Best Practice introduced by
The property category does not exist on Hyde\Framework\Contracts\AbstractMarkdownPage. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property category does not exist on Hyde\Framework\Contracts\AbstractPage. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property category does not exist on Hyde\Framework\Models\Pages\BladePage. Since you implemented __set, consider adding a @property annotation.
Loading history...
39
        }
40
    }
41
42
    protected function get(): AbstractPage
43
    {
44
        return $this->page;
45
    }
46
47
    protected function getDocumentationPageCategory(): ?string
48
    {
49
        // If the documentation page is in a subdirectory,
50
        // then we can use that as the category name.
51
        // Otherwise, we look in the front matter.
52
53
        return str_contains($this->page->identifier, '/')
54
            ? Str::before($this->page->identifier, '/')
55
            : $this->page->matter('category');
56
    }
57
}
58