Test Failed
Push — master ( ac7470...82dec8 )
by Caen
14:29 queued 12s
created

DocumentationPage::getDocumentationPageCategory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models\Pages;
4
5
use Hyde\Framework\Concerns\HasTableOfContents;
6
use Hyde\Framework\Contracts\AbstractMarkdownPage;
7
use Hyde\Framework\Contracts\RouteContract;
8
use Hyde\Framework\Models\Route;
9
use Illuminate\Support\Str;
10
11
class DocumentationPage extends AbstractMarkdownPage
12
{
13
    use HasTableOfContents;
14
15
    public static string $sourceDirectory = '_docs';
16
    public static string $outputDirectory = 'docs';
17
    public static string $template = 'hyde::layouts/docs';
18
19
    /**
20
     * The sidebar category group, if any.
21
     */
22
    public ?string $category;
23
24
    public function __construct(array $matter = [], string $body = '', string $title = '', string $slug = '')
25
    {
26
        parent::__construct($matter, $body, $title, $slug);
27
        $this->category = $this->getDocumentationPageCategory();
28
    }
29
30
    protected function getDocumentationPageCategory(): ?string
31
    {
32
        if (str_contains($this->slug, '/')) {
33
            return Str::before($this->slug, '/');
34
        }
35
36
        return $this->matter['category'] ?? null;
37
    }
38
39
    /** @inheritDoc */
40
    public function getCurrentPagePath(): string
41
    {
42
        return trim(static::getOutputDirectory().'/'.basename($this->slug), '/');
43
    }
44
45
    /** @internal */
46
    public function getOnlineSourcePath(): string|false
47
    {
48
        if (config('docs.source_file_location_base') === null) {
49
            return false;
50
        }
51
52
        return trim(config('docs.source_file_location_base'), '/').'/'.$this->slug.'.md';
53
    }
54
55
    public static function home(): ?RouteContract
56
    {
57
        return Route::exists('docs/index') ? Route::get('docs/index') : null;
58
    }
59
60
    public static function hasTableOfContents(): bool
61
    {
62
        return config('docs.table_of_contents.enabled', true);
63
    }
64
}
65