Completed
Push — master ( efdc7e...8e7c5b )
by Maarten
01:54 queued 12s
created

ModelMapper::getClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Services;
4
5
use SplFileInfo;
6
use RecursiveDirectoryIterator;
7
use RecursiveIteratorIterator;
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
        $files = new RecursiveIteratorIterator(
23
            new RecursiveDirectoryIterator(app_path()), RecursiveIteratorIterator::SELF_FIRST
24
        );
25
26
        foreach ($files as $file) {
27
            if (self::readablePhp($file)) {
28
                $class = self::getClassName($file);
29
30
                if (is_subclass_of($class, Model::class)) {
31
                    self::$models[] = $class;
32
                }
33
            }
34
        }
35
36
        return self::$models;
37
    }
38
39
    /**
40
     * @param SplFileInfo $file
41
     * @return bool
42
     */
43
    private static function readablePhp(SplFileInfo $file): bool
44
    {
45
        return
46
            $file->isReadable()
47
            && $file->isFile()
48
            && mb_strtolower($file->getExtension()) === 'php';
49
    }
50
}
51