Passed
Push — master ( eb8cff...c3a046 )
by Caen
03:04 queued 14s
created

FileCollection::discoverFilesFor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Hyde\Framework\Foundation;
4
5
use Hyde\Framework\Contracts\AbstractPage;
6
use Hyde\Framework\Helpers\Features;
7
use Hyde\Framework\Models\File;
0 ignored issues
show
Bug introduced by
The type Hyde\Framework\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...
8
use Hyde\Framework\Models\Pages\BladePage;
9
use Hyde\Framework\Models\Pages\DocumentationPage;
10
use Hyde\Framework\Models\Pages\MarkdownPage;
11
use Hyde\Framework\Models\Pages\MarkdownPost;
12
use Hyde\Framework\Services\DiscoveryService;
13
14
/**
15
 * @see \Hyde\Framework\Foundation\FileCollection
16
 */
17
final class FileCollection extends BaseSystemCollection
18
{
19
    public function getSourceFiles(?string $pageClass = null): self
20
    {
21
        return ! $pageClass ? $this->getAllSourceFiles() : $this->getSourceFilesFor($pageClass);
22
    }
23
24
    public function getAllSourceFiles(): self
25
    {
26
        return $this->filter(function (File $file) {
27
            return $file->belongsTo !== null;
28
        });
29
    }
30
31
    public function getSourceFilesFor(string $pageClass): self
32
    {
33
        return $this->filter(function (File $file) use ($pageClass): bool {
34
            return $file->belongsTo() === $pageClass;
35
        });
36
    }
37
38
    public function getMediaFiles(): self
39
    {
40
        return $this->filter(function (File $file): bool {
41
            return str_starts_with($file, '_media');
42
        });
43
    }
44
45
    protected function runDiscovery(): self
46
    {
47
        if (Features::hasBladePages()) {
48
            $this->discoverFilesFor(BladePage::class);
49
        }
50
51
        if (Features::hasMarkdownPages()) {
52
            $this->discoverFilesFor(MarkdownPage::class);
53
        }
54
55
        if (Features::hasBlogPosts()) {
56
            $this->discoverFilesFor(MarkdownPost::class);
57
        }
58
59
        if (Features::hasDocumentationPages()) {
60
            $this->discoverFilesFor(DocumentationPage::class);
61
        }
62
63
        $this->discoverMediaAssetFiles();
64
65
        return $this;
66
    }
67
68
    /** @param string<AbstractPage> $pageClass */
69
    protected function discoverFilesFor(string $pageClass): void
70
    {
71
        // Scan the source directory, and directories therein, for files that match the model's file extension.
72
        foreach (glob($this->kernel->path($pageClass::qualifyBasename('{*,**/*}')), GLOB_BRACE) as $filepath) {
0 ignored issues
show
Bug introduced by
The method path() does not exist on Hyde\Framework\Contracts\HydeKernelContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\HydeKernelContract. ( Ignorable by Annotation )

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

72
        foreach (glob($this->kernel->/** @scrutinizer ignore-call */ path($pageClass::qualifyBasename('{*,**/*}')), GLOB_BRACE) as $filepath) {
Loading history...
73
            if (! str_starts_with(basename($filepath), '_')) {
74
                $this->put($this->kernel->pathToRelative($filepath), File::make($filepath)->belongsTo($pageClass));
0 ignored issues
show
Bug introduced by
The method pathToRelative() does not exist on Hyde\Framework\Contracts\HydeKernelContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\HydeKernelContract. ( Ignorable by Annotation )

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

74
                $this->put($this->kernel->/** @scrutinizer ignore-call */ pathToRelative($filepath), File::make($filepath)->belongsTo($pageClass));
Loading history...
75
            }
76
        }
77
    }
78
79
    protected function discoverMediaAssetFiles(): void
80
    {
81
        foreach (DiscoveryService::getMediaAssetFiles() as $filepath) {
82
            $this->put($this->kernel->pathToRelative($filepath), File::make($filepath));
83
        }
84
    }
85
}
86