Passed
Push — master ( b9dd51...a77e6b )
by Caen
03:22 queued 13s
created

RebuildPageCommand.php$0 ➔ makeBuildTask()   A

Complexity

Conditions 3

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 46
rs 9.1781
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A RebuildPageCommand.php$0 ➔ handle() 0 5 1
A RebuildPageCommand.php$0 ➔ printFinishMessage() 0 5 1
A RebuildPageCommand.php$0 ➔ __construct() 0 4 1
A RebuildPageCommand.php$0 ➔ validate() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Commands;
6
7
use Exception;
8
use Hyde\Console\Concerns\Command;
9
use Hyde\Foundation\Facades\Pages;
10
use Hyde\Framework\Features\BuildTasks\BuildTask;
11
use Hyde\Framework\Services\BuildService;
12
use Hyde\Framework\Services\RebuildService;
13
use Hyde\Hyde;
14
use Illuminate\Console\OutputStyle;
15
use function dirname;
16
use function file_exists;
17
use function in_array;
18
use function str_replace;
19
use function unslash;
20
21
/**
22
 * Hyde Command to build a single static site file.
23
 *
24
 * @see \Hyde\Framework\Testing\Feature\Commands\RebuildPageCommand
25
 */
26
class RebuildPageCommand extends Command
27
{
28
    /** @var string */
29
    protected $signature = 'rebuild {path : The relative file path (example: _posts/hello-world.md)}';
30
31
    /** @var string */
32
    protected $description = 'Run the static site builder for a single file';
33
34
    public function handle(): int
35
    {
36
        if ($this->argument('path') === Hyde::getMediaDirectory()) {
37
            (new BuildService($this->getOutput()))->transferMediaAssets();
38
39
            $this->info('All done!');
40
41
            return Command::SUCCESS;
42
        }
43
44
        return $this->makeBuildTask($this->output, $this->getNormalizedPathString())->run();
45
    }
46
47
    protected function getNormalizedPathString(): string
48
    {
49
        return str_replace('\\', '/', unslash($this->argument('path')));
50
    }
51
52
    protected function makeBuildTask(OutputStyle $output, string $path): BuildTask
53
    {
54
        return new class($output, $path) extends BuildTask
55
        {
56
            public static string $message = 'Rebuilding page';
57
58
            protected string $path;
59
60
            public function __construct(OutputStyle $output, string $path)
61
            {
62
                $this->output = $output;
63
                $this->path = $path;
64
            }
65
66
            public function handle(): void
67
            {
68
                $this->validate();
69
70
                (new RebuildService($this->path))->execute();
71
            }
72
73
            public function printFinishMessage(): void
74
            {
75
                $this->createdSiteFile(Command::fileLink(
76
                    Pages::getPage($this->path)->getOutputPath()
0 ignored issues
show
Bug introduced by
The method getPage() does not exist on Hyde\Foundation\Facades\Pages. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

76
                    Pages::/** @scrutinizer ignore-call */ 
77
                           getPage($this->path)->getOutputPath()
Loading history...
77
                ))->withExecutionTime();
78
            }
79
80
            protected function validate(): void
81
            {
82
                $directory = Hyde::pathToRelative(dirname($this->path));
83
84
                $directories = [
85
                    Hyde::pathToRelative(Hyde::getBladePagePath()),
86
                    Hyde::pathToRelative(Hyde::getBladePagePath()),
87
                    Hyde::pathToRelative(Hyde::getMarkdownPagePath()),
88
                    Hyde::pathToRelative(Hyde::getMarkdownPostPath()),
89
                    Hyde::pathToRelative(Hyde::getDocumentationPagePath()),
90
                ];
91
92
                if (! in_array($directory, $directories)) {
93
                    throw new Exception("Path [$this->path] is not in a valid source directory.", 400);
94
                }
95
96
                if (! file_exists(Hyde::path($this->path))) {
97
                    throw new Exception("File [$this->path] not found.", 404);
98
                }
99
            }
100
        };
101
    }
102
}
103