ValidationService   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 26

10 Methods

Rating   Name   Duplication   Size   Complexity  
A checks() 0 11 3
A check_a_torchlight_api_token_is_set() 0 13 3
A run() 0 6 1
A check_site_has_a_404_page() 0 10 3
A check_site_has_a_base_url_set() 0 9 2
A check_site_has_an_index_page() 0 10 3
A check_site_has_an_app_css_stylesheet() 0 9 3
A check_for_conflicts_between_blade_and_markdown_pages() 0 13 2
A check_documentation_site_has_an_index_page() 0 22 5
A check_validators_can_run() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use Hyde\Hyde;
8
use Hyde\Facades\Config;
9
use Hyde\Facades\Features;
10
use Hyde\Pages\BladePage;
11
use Hyde\Pages\MarkdownPage;
12
use Hyde\Pages\DocumentationPage;
13
use Hyde\Enums\Feature;
14
use Hyde\Support\Filesystem\MediaFile;
15
use Hyde\Support\Models\ValidationResult as Result;
16
17
use function count;
18
use function get_class_methods;
19
use function array_intersect;
20
use function file_exists;
21
use function implode;
22
use function sprintf;
23
use function str_starts_with;
24
25
class ValidationService
26
{
27
    /** @return string[] */
28
    public static function checks(): array
29
    {
30
        $service = new self();
31
        $checks = [];
32
        foreach (get_class_methods($service) as $method) {
33
            if (str_starts_with($method, 'check_')) {
34
                $checks[] = $method;
35
            }
36
        }
37
38
        return $checks;
39
    }
40
41
    public function run(string $check): Result
42
    {
43
        $result = new Result;
44
        $this->{$check}($result);
45
46
        return $result;
47
    }
48
49
    public function check_validators_can_run(Result $result): Result
50
    {
51
        // Runs a rather useless check, but which forces the class to load, thus preventing skewed test results
52
        // as the first test generally takes a little longer to run.
53
        return $result->pass('Validators can run');
54
    }
55
56
    public function check_site_has_a_404_page(Result $result): Result
57
    {
58
        if (file_exists(MarkdownPage::path('404.md'))
59
            || file_exists(BladePage::path('404.blade.php'))
60
        ) {
61
            return $result->pass('Your site has a 404 page');
62
        }
63
64
        return $result->fail('Could not find an 404.md or 404.blade.php file!')
65
            ->withTip('You can publish the default one using `php hyde publish:views`');
66
    }
67
68
    public function check_site_has_an_index_page(Result $result): Result
69
    {
70
        if (file_exists(MarkdownPage::path('index.md'))
71
            || file_exists(BladePage::path('index.blade.php'))
72
        ) {
73
            return $result->pass('Your site has an index page');
74
        }
75
76
        return $result->fail('Could not find an index.md or index.blade.php file!')
77
            ->withTip('You can publish the one of the built in templates using `php hyde publish:homepage`');
78
    }
79
80
    public function check_documentation_site_has_an_index_page(Result $result): Result
81
    {
82
        if (! Features::hasDocumentationPages()) {
83
            return $result->skip('Does documentation site have an index page?')
84
                ->withTip('Skipped because: The documentation page feature is disabled in config');
85
        }
86
87
        if (count(DocumentationPage::files()) === 0) {
88
            return $result->skip('Does documentation site have an index page?')
89
                ->withTip('Skipped because: There are no documentation pages');
90
        }
91
92
        if (file_exists(DocumentationPage::path('index.md'))) {
93
            return $result->pass('Your documentation site has an index page');
94
        }
95
96
        if (file_exists(DocumentationPage::path('README.md'))) {
97
            return $result->fail('Could not find an index.md file in the _docs directory!')
98
                ->withTip('However, a _docs/readme.md file was found. A suggestion would be to copy the _docs/readme.md to _docs/index.md.');
99
        }
100
101
        return $result->fail('Could not find an index.md file in the _docs directory!');
102
    }
103
104
    public function check_site_has_an_app_css_stylesheet(Result $result): Result
105
    {
106
        if (file_exists(MediaFile::outputPath('/app.css')) || file_exists(MediaFile::sourcePath('app.css'))) {
107
            return $result->pass('Your site has an app.css stylesheet');
108
        }
109
110
        return $result->fail(sprintf('Could not find an app.css file in the %s or %s directory!',
111
            Hyde::pathToRelative(MediaFile::outputPath()), Hyde::getMediaDirectory()
0 ignored issues
show
Bug introduced by
The method pathToRelative() does not exist on Hyde\Hyde. 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

111
            Hyde::/** @scrutinizer ignore-call */ 
112
                  pathToRelative(MediaFile::outputPath()), Hyde::getMediaDirectory()
Loading history...
Bug introduced by
The method getMediaDirectory() does not exist on Hyde\Hyde. 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

111
            Hyde::pathToRelative(MediaFile::outputPath()), Hyde::/** @scrutinizer ignore-call */ getMediaDirectory()
Loading history...
112
        ))->withTip('You may need to run `npm run dev`.`');
113
    }
114
115
    public function check_site_has_a_base_url_set(Result $result): Result
116
    {
117
        if (Hyde::hasSiteUrl()) {
0 ignored issues
show
Bug introduced by
The method hasSiteUrl() does not exist on Hyde\Hyde. 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

117
        if (Hyde::/** @scrutinizer ignore-call */ hasSiteUrl()) {
Loading history...
118
            return $result->pass('Your site has a base URL set')
119
                ->withTip('This will allow Hyde to generate canonical URLs, sitemaps, RSS feeds, and more.');
120
        }
121
122
        return $result->fail('Could not find a site URL in the config or .env file!')
123
            ->withTip('Adding it may improve SEO as it allows Hyde to generate canonical URLs, sitemaps, and RSS feeds');
124
    }
125
126
    public function check_a_torchlight_api_token_is_set(Result $result): Result
127
    {
128
        if (! Features::has(Feature::Torchlight)) {
129
            return $result->skip('Check a Torchlight API token is set')
130
                ->withTip('Torchlight is an API for code syntax highlighting. You can enable it in the Hyde config.');
131
        }
132
133
        if (Config::getNullableString('torchlight.token') !== null) {
134
            return $result->pass('Your site has a Torchlight API token set');
135
        }
136
137
        return $result->fail('Torchlight is enabled in the config, but an API token could not be found in the .env file!')
138
            ->withTip('Torchlight is an API for code syntax highlighting. You can get a free token at torchlight.dev.');
139
    }
140
141
    public function check_for_conflicts_between_blade_and_markdown_pages(Result $result): Result
142
    {
143
        $conflicts = array_intersect(
144
            MarkdownPage::files(),
145
            BladePage::files()
146
        );
147
148
        if (count($conflicts)) {
149
            return $result->fail('Found naming conflicts between Markdown and Blade files: '.implode(', ', $conflicts))
150
                ->withTip('This may cause on of them being immediately overwritten by the other.');
151
        }
152
153
        return $result->pass('No naming conflicts found between Blade and Markdown pages');
154
    }
155
}
156