Passed
Push — master ( 8492cc...15f9ee )
by Caen
05:33 queued 02:01
created

CreatesNewPageSourceFile::parseTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Concerns\InteractsWithDirectories;
6
use Hyde\Framework\Exceptions\FileConflictException;
7
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
8
use Hyde\Framework\Hyde;
9
use Hyde\Framework\Models\Pages\BladePage;
10
use Hyde\Framework\Models\Pages\DocumentationPage;
11
use Hyde\Framework\Models\Pages\MarkdownPage;
12
use Illuminate\Support\Str;
13
14
/**
15
 * Scaffold a new Markdown, Blade, or documentation page.
16
 *
17
 * @see \Hyde\Framework\Testing\Feature\Actions\CreatesNewPageSourceFileTest
18
 */
19
class CreatesNewPageSourceFile
20
{
21
    use InteractsWithDirectories;
22
23
    public string $title;
24
    public string $slug;
25
    public string $outputPath;
26
    public string $subDir = '';
27
28
    public function __construct(string $title, string $type = MarkdownPage::class, public bool $force = false)
29
    {
30
        $this->title = $this->parseTitle($title);
31
        $this->slug = $this->parseSlug($title);
32
33
        $this->createPage($type);
34
    }
35
36
    public function parseTitle(string $title): string
37
    {
38
        return Str::afterLast($title, '/');
39
    }
40
41
    public function parseSlug(string $title): string
42
    {
43
        if (str_contains($title, '/')) {
44
            $this->subDir = Str::beforeLast($title, '/').'/';
45
        }
46
47
        return Str::slug(basename($title));
48
    }
49
50
    public function canSaveFile(string $path): void
51
    {
52
        if (file_exists($path) && ! $this->force) {
53
            throw new FileConflictException($path);
54
        }
55
    }
56
57
    public function createPage(string $type): int|false
58
    {
59
        $subDir = $this->subDir;
60
        if ($subDir !== '') {
61
            $subDir = '/'.rtrim($subDir, '/\\');
62
        }
63
64
        if ($type === MarkdownPage::class) {
65
            $this->needsDirectory(MarkdownPage::getSourceDirectory().$subDir);
66
67
            return $this->createMarkdownFile();
68
        }
69
        if ($type === BladePage::class) {
70
            $this->needsDirectory(BladePage::getSourceDirectory().$subDir);
71
72
            return $this->createBladeFile();
73
        }
74
75
        if ($type === DocumentationPage::class) {
76
            $this->needsDirectory(DocumentationPage::getSourceDirectory().$subDir);
77
78
            return $this->createDocumentationFile();
79
        }
80
81
        throw new UnsupportedPageTypeException('The page type must be either "markdown", "blade", or "documentation"');
82
    }
83
84
    public function createMarkdownFile(): int|false
85
    {
86
        $this->outputPath = Hyde::path("_pages/$this->subDir$this->slug.md");
87
88
        $this->canSaveFile($this->outputPath);
89
90
        return file_put_contents(
91
            $this->outputPath,
92
            "---\ntitle: $this->title\n---\n\n# $this->title\n"
93
        );
94
    }
95
96
    public function createBladeFile(): int|false
97
    {
98
        $this->outputPath = Hyde::path("_pages/$this->subDir$this->slug.blade.php");
99
100
        $this->canSaveFile($this->outputPath);
101
102
        return file_put_contents(
103
            $this->outputPath,
104
            <<<EOF
105
@extends('hyde::layouts.app')
106
@section('content')
107
@php(\$title = "$this->title")
108
109
<main class="mx-auto max-w-7xl py-16 px-8">
110
	<h1 class="text-center text-3xl font-bold">$this->title</h1>
111
</main>
112
113
@endsection
114
115
EOF
116
        );
117
    }
118
119
    public function createDocumentationFile(): int|false
120
    {
121
        $this->outputPath = Hyde::path("_docs/$this->subDir$this->slug.md");
122
123
        $this->canSaveFile($this->outputPath);
124
125
        return file_put_contents(
126
            $this->outputPath,
127
            "# $this->title\n"
128
        );
129
    }
130
}
131