ModelMapper::readablePhp()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Services;
4
5
use SplFileInfo;
6
use RecursiveIteratorIterator;
7
use RecursiveDirectoryIterator;
8
use Illuminate\Database\Eloquent\Model;
9
10
class ModelMapper extends ClassReader
11
{
12
    public static $models = [];
13
14
    /**
15
     * Maps subclasses of Illuminate\Database\Eloquent\Model
16
     * found in app_path()
17
     *
18
     * @return array
19
     */
20
    public static function map(): array
21
    {
22
        $paths = config('schematics.model.paths', [app_path()]);
23
24
        foreach ($paths as $path) {
25
            $files = new RecursiveIteratorIterator(
26
                new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST
27
            );
28
29
            foreach ($files as $file) {
30
                if (self::readablePhp($file)) {
31
                    $class = self::getClassName($file);
32
33
                    if (is_subclass_of($class, Model::class)) {
34
                        self::$models[] = $class;
35
                    }
36
                }
37
            }
38
        }
39
40
        return self::$models;
41
    }
42
43
    /**
44
     * @param SplFileInfo $file
45
     * @return bool
46
     */
47
    private static function readablePhp(SplFileInfo $file): bool
48
    {
49
        return
50
            $file->isReadable()
51
            && $file->isFile()
52
            && mb_strtolower($file->getExtension()) === 'php';
53
    }
54
}
55