HasMediaFiles::assets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Concerns;
6
7
use Hyde\Hyde;
8
use Hyde\Facades\Config;
9
use Hyde\Facades\Filesystem;
10
use Hyde\Support\Filesystem\MediaFile;
11
use Illuminate\Support\Collection;
12
13
use function collect;
14
15
/**
16
 * @internal Single-use trait for the Filesystem class.
17
 *
18
 * @see \Hyde\Foundation\Kernel\Filesystem
19
 */
20
trait HasMediaFiles
21
{
22
    /** @var Collection<string, \Hyde\Support\Filesystem\MediaFile> The Collection keys are the filenames relative to the _media/ directory */
23
    protected Collection $assets;
24
25
    /**
26
     * Get all media files in the project.
27
     *
28
     * @return Collection<string, \Hyde\Support\Filesystem\MediaFile>
29
     */
30
    public function assets(): Collection
31
    {
32
        return $this->assets ??= static::discoverMediaFiles();
33
    }
34
35
    protected static function discoverMediaFiles(): Collection
36
    {
37
        return collect(static::getMediaFiles())->mapWithKeys(function (string $path): array {
0 ignored issues
show
Bug introduced by
static::getMediaFiles() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

37
        return collect(/** @scrutinizer ignore-type */ static::getMediaFiles())->mapWithKeys(function (string $path): array {
Loading history...
38
            $file = MediaFile::make($path);
39
40
            return [$file->getIdentifier() => $file];
41
        });
42
    }
43
44
    protected static function getMediaFiles(): array
45
    {
46
        return Filesystem::findFiles(Hyde::getMediaDirectory(),
0 ignored issues
show
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

46
        return Filesystem::findFiles(Hyde::/** @scrutinizer ignore-call */ getMediaDirectory(),
Loading history...
47
            Config::getArray('hyde.media_extensions', MediaFile::EXTENSIONS), recursive: true
48
        )->all();
49
    }
50
}
51