Passed
Push — master ( e1c92c...019c74 )
by Caen
03:05 queued 13s
created

DiscoveryService   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 29
Bugs 1 Features 0
Metric Value
wmc 18
eloc 25
dl 0
loc 113
rs 10
c 29
b 1
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getSourceFileListForModel() 0 9 3
A parseConfiguredMediaExtensions() 0 5 2
A getBladePageFiles() 0 3 1
A getMediaGlobPattern() 0 4 1
A pathToIdentifier() 0 5 1
A getMarkdownPageFiles() 0 3 1
A removeSpaces() 0 3 1
A getModelFileExtension() 0 3 1
A getModelSourceDirectory() 0 3 1
A getMarkdownPostFiles() 0 3 1
A getDocumentationPageFiles() 0 3 1
A createClickableFilepath() 0 7 2
A getMediaAssetFiles() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use function class_exists;
8
use function config;
9
use function glob;
10
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
11
use Hyde\Hyde;
12
use Hyde\Pages\BladePage;
13
use Hyde\Pages\Concerns\HydePage;
14
use Hyde\Pages\DocumentationPage;
15
use Hyde\Pages\MarkdownPage;
16
use Hyde\Pages\MarkdownPost;
17
use Hyde\Support\Models\File;
0 ignored issues
show
Bug introduced by
The type Hyde\Support\Models\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Illuminate\Support\Str;
19
use function implode;
20
use function is_array;
21
use function is_subclass_of;
22
use function realpath;
23
use function sprintf;
24
use function str_replace;
25
use function unslash;
26
27
/**
28
 * The core service that powers all HydePHP file auto-discovery.
29
 *
30
 * Contains service methods to return helpful collections of arrays and lists,
31
 * and provides helper methods for source file auto-discovery used in the site
32
 * building process to determine where files are located and how to parse them.
33
 *
34
 * @see \Hyde\Framework\Testing\Feature\DiscoveryServiceTest
35
 */
36
class DiscoveryService
37
{
38
    public const DEFAULT_MEDIA_EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
39
40
    public static function getBladePageFiles(): array
41
    {
42
        return static::getSourceFileListForModel(BladePage::class);
43
    }
44
45
    public static function getMarkdownPageFiles(): array
46
    {
47
        return static::getSourceFileListForModel(MarkdownPage::class);
48
    }
49
50
    public static function getMarkdownPostFiles(): array
51
    {
52
        return static::getSourceFileListForModel(MarkdownPost::class);
53
    }
54
55
    public static function getDocumentationPageFiles(): array
56
    {
57
        return static::getSourceFileListForModel(DocumentationPage::class);
58
    }
59
60
    /**
61
     * Supply a model::class constant and get a list of all the existing source file base names.
62
     *
63
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $model
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
64
     *
65
     * @throws \Hyde\Framework\Exceptions\UnsupportedPageTypeException
66
     *
67
     * @example Usage: DiscoveryService::getSourceFileListForModel(BladePage::class)
68
     * @example Returns: ['index', 'about', 'contact']
69
     */
70
    public static function getSourceFileListForModel(string $model): array
71
    {
72
        if (! class_exists($model) || ! is_subclass_of($model, HydePage::class)) {
73
            throw new UnsupportedPageTypeException($model);
74
        }
75
76
        return Hyde::files()->getSourceFiles($model)->flatten()->map(function (File $file) use ($model): string {
77
            return static::pathToIdentifier($model, $file->withoutDirectoryPrefix());
78
        })->toArray();
79
    }
80
81
    /** @param class-string<\Hyde\Pages\Concerns\HydePage> $model */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
82
    public static function getModelFileExtension(string $model): string
83
    {
84
        return $model::fileExtension();
85
    }
86
87
    /** @param class-string<\Hyde\Pages\Concerns\HydePage> $model */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
88
    public static function getModelSourceDirectory(string $model): string
89
    {
90
        return $model::sourceDirectory();
91
    }
92
93
    /**
94
     * Create a filepath that can be opened in the browser from a terminal.
95
     */
96
    public static function createClickableFilepath(string $filepath): string
97
    {
98
        if (realpath($filepath) === false) {
99
            return $filepath;
100
        }
101
102
        return 'file://'.str_replace('\\', '/', realpath($filepath));
103
    }
104
105
    /**
106
     * Format a filename to an identifier for a given model. Unlike the basename function, any nested paths
107
     * within the source directory are retained in order to satisfy the page identifier definition.
108
     *
109
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $model
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
110
     * @param  string  $filepath  Example: index.blade.php
111
     * @return string Example: index
112
     */
113
    public static function pathToIdentifier(string $model, string $filepath): string
114
    {
115
        return unslash(Str::between(Hyde::pathToRelative($filepath),
116
            $model::$sourceDirectory.'/',
0 ignored issues
show
Bug introduced by
The property sourceDirectory does not exist on string.
Loading history...
117
            $model::$fileExtension)
0 ignored issues
show
Bug introduced by
The property fileExtension does not exist on string.
Loading history...
118
        );
119
    }
120
121
    /**
122
     * Get all the Media asset file paths.
123
     * Returns a full file path, unlike the other get*List methods.
124
     *
125
     * @return array<string> An array of absolute file paths.
126
     */
127
    public static function getMediaAssetFiles(): array
128
    {
129
        return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
130
    }
131
132
    protected static function getMediaGlobPattern(): string
133
    {
134
        return sprintf('_media/*.{%s}', static::parseConfiguredMediaExtensions(
135
            config('hyde.media_extensions', self::DEFAULT_MEDIA_EXTENSIONS)
136
        ));
137
    }
138
139
    protected static function parseConfiguredMediaExtensions(string|array|null $extensions): string
140
    {
141
        return is_array($extensions)
0 ignored issues
show
introduced by
The condition is_array($extensions) is always true.
Loading history...
142
            ? implode(',', $extensions)
143
            : static::removeSpaces((string) $extensions);
144
    }
145
146
    protected static function removeSpaces(string $string): string
147
    {
148
        return str_replace(' ', '', $string);
149
    }
150
}
151