Passed
Push — master ( 0503bd...6a1d06 )
by Caen
03:16 queued 13s
created

FileCollection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 16
c 5
b 0
f 0
dl 0
loc 63
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A discoverMediaAssetFiles() 0 4 2
A getMediaFiles() 0 3 1
A runDiscovery() 0 14 3
A getSourceFiles() 0 3 2
A discoverFilesFor() 0 6 3
A getAllSourceFiles() 0 3 1
A getSourceFilesFor() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation;
6
7
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8
use Hyde\Framework\Services\DiscoveryService;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Filesystem\MediaFile;
11
use Hyde\Support\Filesystem\ProjectFile;
12
use Hyde\Support\Filesystem\SourceFile;
13
14
/**
15
 * The FileCollection contains all the discovered source and media files,
16
 * and thus has an integral role in the Hyde Auto Discovery process.
17
 *
18
 * This class is stored as a singleton in the HydeKernel.
19
 * You would commonly access it via one of the facades:
20
 *
21
 * @see \Hyde\Foundation\Facades\FileCollection
22
 * @see \Hyde\Hyde::files()
23
 */
24
final class FileCollection extends BaseFoundationCollection
25
{
26
    /**
27
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>|null  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>|null.
Loading history...
28
     * @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile>
29
     */
30
    public function getSourceFiles(?string $pageClass = null): self
31
    {
32
        return ! $pageClass ? $this->getAllSourceFiles() : $this->getSourceFilesFor($pageClass);
33
    }
34
35
    /**
36
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
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...
37
     * @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile>
38
     */
39
    public function getSourceFilesFor(string $pageClass): self
40
    {
41
        return $this->getAllSourceFiles()->where(fn (SourceFile $file): bool => $file->model === $pageClass);
42
    }
43
44
    /** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile> */
45
    public function getAllSourceFiles(): self
46
    {
47
        return $this->where(fn (ProjectFile $file): bool => $file instanceof SourceFile);
48
    }
49
50
    /** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\MediaFile> */
51
    public function getMediaFiles(): self
52
    {
53
        return $this->where(fn (ProjectFile $file): bool => $file instanceof MediaFile);
54
    }
55
56
    protected function runDiscovery(): self
57
    {
58
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
59
            $this->discoverFilesFor($pageClass);
60
        }
61
62
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
63
        foreach ($this->kernel->getRegisteredExtensions() as $extension) {
64
            $extension::discoverFiles($this);
65
        }
66
67
        $this->discoverMediaAssetFiles();
68
69
        return $this;
70
    }
71
72
    /** @param class-string<HydePage> $pageClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<HydePage>.
Loading history...
73
    protected function discoverFilesFor(string $pageClass): void
74
    {
75
        // Scan the source directory, and directories therein, for files that match the model's file extension.
76
        foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) {
77
            if (! str_starts_with(basename((string) $filepath), '_')) {
78
                $this->put($this->kernel->pathToRelative($filepath), SourceFile::make($filepath, $pageClass));
0 ignored issues
show
Bug introduced by
$this->kernel->pathToRelative($filepath) of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

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

78
                $this->put(/** @scrutinizer ignore-type */ $this->kernel->pathToRelative($filepath), SourceFile::make($filepath, $pageClass));
Loading history...
79
            }
80
        }
81
    }
82
83
    protected function discoverMediaAssetFiles(): void
84
    {
85
        foreach (DiscoveryService::getMediaAssetFiles() as $filepath) {
86
            $this->put($this->kernel->pathToRelative($filepath), MediaFile::make($filepath));
0 ignored issues
show
Bug introduced by
$this->kernel->pathToRelative($filepath) of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

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

86
            $this->put(/** @scrutinizer ignore-type */ $this->kernel->pathToRelative($filepath), MediaFile::make($filepath));
Loading history...
87
        }
88
    }
89
}
90