Passed
Pull Request — master (#244)
by Alex
22:10
created

ClassReflectionHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
c 1
b 0
f 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCandidateControllers() 0 15 2
A getAppNamespace() 0 8 2
A getCandidateModels() 0 9 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 1/06/20
6
 * Time: 12:36 AM
7
 */
8
9
namespace AlgoWeb\PODataLaravel\Models;
10
11
use Cruxinator\ClassFinder\ClassFinder;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Support\Facades\App;
14
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerTrait;
15
use Illuminate\Routing\Controller;
16
17
/**
18
 * Class ClassReflectionHelper
19
 * @package AlgoWeb\PODataLaravel\Models
20
 */
21
abstract class ClassReflectionHelper
22
{
23
    /**
24
     * @throws \Exception
25
     * @return string[]
26
     */
27
    public static function getCandidateModels(): array
28
    {
29
        return ClassFinder::getClasses(
30
            static::getAppNamespace(),
31
            function ($className) {
32
                return in_array(MetadataTrait::class, class_uses($className)) &&
33
                       is_subclass_of($className, Model::class);
34
            },
35
            true
36
        );
37
    }
38
39
    /**
40
     * @return array
41
     * @throws \Exception
42
     */
43
    public static function getCandidateControllers(): array
44
    {
45
        $classes = ClassFinder::getClasses(
46
            static::getAppNamespace(),
47
            function ($className) {
48
                return in_array(MetadataControllerTrait::class, class_uses($className)) &&
49
                       (App::make($className) instanceof Controller);
50
            },
51
            true
52
        );
53
        $ends = array_reduce($classes, function ($carry, $item) {
54
            $carry[] = App::make($item);
55
            return $carry;
56
        }, []);
57
        return $ends;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public static function getAppNamespace(): string
64
    {
65
        try {
66
            $startName = App::getNamespace();
67
        } catch (\Exception $e) {
68
            $startName = 'App';
69
        }
70
        return $startName;
71
    }
72
}
73