Passed
Push — master ( 3684ce...bd7ad1 )
by Caen
03:24 queued 12s
created

RebuildStaticSiteCommand::handleException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\PageCollection;
10
use Hyde\Framework\Services\BuildService;
11
use Hyde\Framework\Services\RebuildService;
12
use Hyde\Hyde;
13
14
/**
15
 * Hyde Command to build a single static site file.
16
 *
17
 * @see \Hyde\Framework\Testing\Feature\Commands\RebuildStaticSiteCommandTest
18
 *
19
 * @todo Refactor to use newer helpers
20
 */
21
class RebuildStaticSiteCommand extends Command
22
{
23
    /** @var string */
24
    protected $signature = 'rebuild
25
        {path : The relative file path (example: _posts/hello-world.md)}';
26
27
    /** @var string */
28
    protected $description = 'Run the static site builder for a single file';
29
30
    /**
31
     * The source path.
32
     */
33
    public string $path;
34
35
    public function handle(): int
36
    {
37
        $time_start = microtime(true);
38
39
        if ($this->argument('path') === '_media') {
40
            (new BuildService($this->getOutput()))->transferMediaAssets();
41
42
            return Command::SUCCESS;
43
        }
44
45
        $this->path = $this->sanitizePathString($this->argument('path'));
46
47
        try {
48
            $this->validate();
49
        } catch (Exception $exception) {
50
            return $this->withException($exception);
51
        }
52
53
        (new RebuildService($this->path))->execute();
54
55
        $time_end = microtime(true);
56
        $execution_time = ($time_end - $time_start);
57
58
        $this->info(sprintf(
59
            'Created %s in %s seconds. (%sms)',
60
            static::createClickableFilepath(PageCollection::getPage($this->path)->getOutputPath()),
0 ignored issues
show
Bug introduced by
The method getPage() does not exist on Hyde\Foundation\Facades\PageCollection. 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

60
            static::createClickableFilepath(PageCollection::/** @scrutinizer ignore-call */ getPage($this->path)->getOutputPath()),
Loading history...
61
            number_format(
62
                $execution_time,
63
                2
64
            ),
65
            number_format(($execution_time * 1000), 2)
66
        ));
67
68
        return Command::SUCCESS;
69
    }
70
71
    /**
72
     * Perform a basic sanitation to strip trailing characters.
73
     */
74
    public function sanitizePathString(string $path): string
75
    {
76
        return str_replace('\\', '/', trim($path, '.\\/'));
77
    }
78
79
    /**
80
     * Validate the path to catch common errors.
81
     *
82
     * @throws Exception
83
     */
84
    public function validate(): void
85
    {
86
        if (! (
87
            str_starts_with($this->path, Hyde::pathToRelative(Hyde::getBladePagePath())) ||
88
            str_starts_with($this->path, Hyde::pathToRelative(Hyde::getMarkdownPagePath())) ||
89
            str_starts_with($this->path, Hyde::pathToRelative(Hyde::getMarkdownPostPath())) ||
90
            str_starts_with($this->path, Hyde::pathToRelative(Hyde::getDocumentationPagePath()))
91
        )) {
92
            throw new Exception("Path [$this->path] is not in a valid source directory.", 400);
93
        }
94
95
        if (! file_exists(Hyde::path($this->path))) {
96
            throw new Exception("File [$this->path] not found.", 404);
97
        }
98
    }
99
100
    /**
101
     * Output the contents of an exception.
102
     *
103
     * @return int Error code
104
     */
105
    public function withException(Exception $exception): int
106
    {
107
        $this->error('Something went wrong!');
108
        $this->warn($exception->getMessage());
109
110
        return (int) $exception->getCode();
111
    }
112
}
113