Test Setup Failed
Pull Request — master (#36)
by Alex
03:04
created

MetadataControllerProvider::getClassMap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
crap 12
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Providers;
4
5
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerTrait;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Support\Facades\Cache;
8
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer;
9
use Illuminate\Support\Facades\App;
10
11
class MetadataControllerProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap the application services.  Post-boot.
15
     *
16 68
     * @return void
17
     */
18 68
    public function boot()
19
    {
20 68
        $isCaching = env('APP_METADATA_CACHING', false);
21
22
        if ($isCaching && Cache::has('metadataControllers')) {
23
            $meta = Cache::get('metadataControllers');
24
            App::instance('metadataControllers', $meta);
1 ignored issue
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\App. Did you maybe mean clearResolvedInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
25
            return;
26 68
        }
27
28 68
        $meta = App::make('metadataControllers');
29 68
30 68
        $Classes = $this->getClassMap();
31 68
        $ends = $this->getCandidateControllers($Classes);
32 68
33 68
        // now process each class that uses the metadata controller trait and stick results in $metamix
34 68
        $metamix = [];
35
        $map = null;
0 ignored issues
show
Unused Code introduced by
$map is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36 68
        foreach ($ends as $end) {
37 68
            $map = $end->getMappings();
38 68
            // verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for
39 68
            // a model can glom onto different controllers
40
            foreach ($map as $key => $lock) {
41 68
                if (!array_key_exists($key, $metamix)) {
42 68
                    // if we haven't yet got a mapping for this model, grab it holus-bolus
43 68
                    $metamix[$key] = $lock;
44 68
                    continue;
45 67
                }
46 67
                // if we do, make sure we aren't re-adding mappings for any of the CRUD verbs
47 68
                foreach ($lock as $barrel => $roll) {
48
                    assert(
49
                        !array_key_exists($barrel, $metamix[$key]),
50 68
                        'Mapping already defined for model '.$key.' and CRUD verb '.$barrel
51
                    );
52
                    $metamix[$key][$barrel] = $roll;
53 68
                }
54 68
            }
55 67
        }
56
57
        $meta->setMetadata($metamix);
58 67
59 67
    }
60
61 67
    /**
62 67
     * Register the application services.  Boot-time only.
63
     *
64
     * @return void
65
     */
66
    public function register()
67
    {
68
        $this->app->singleton('metadataControllers', function ($app) {
1 ignored issue
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
            return new MetadataControllerContainer();
70
        });
71
    }
72
73 67
    /**
74 68
     * @param $Classes
75
     * @return array
76 68
     * @throws \Exception
77
     */
78 68
    protected function getCandidateControllers($Classes)
79
    {
80
        $ends = [];
81
        $startName = defined('PODATA_LARAVEL_APP_ROOT_NAMESPACE') ? PODATA_LARAVEL_APP_ROOT_NAMESPACE : "App";
82
        foreach ($Classes as $name) {
83
            // not in app namespace, keep moving
84
            if (!\Illuminate\Support\Str::startsWith($name, $startName)) {
85
                continue;
86
            }
87 68
            // if class doesn't exist (for whatever reason), skip it now rather than muck about later
88 68
            if (!class_exists($name)) {
89 68
                continue;
90 68
            }
91
            try {
92
                if (in_array(MetadataControllerTrait::class, class_uses($name, false))) {
93
                    $ends[] = App::make($name);
94
                }
95
            } catch (\Exception $e) {
96
                if (!App::runningInConsole()) {
97
                    throw $e;
98
                }
99
                // Squash exceptions thrown here when running from CLI so app can continue booting
100
            }
101
        }
102
        return $ends;
103
    }
104
105
    /**
106
     * @param $classMap
107
     * @return mixed
108
     */
109
    protected function getClassMap()
110
    {
111
        $classes = get_declared_classes();
112
        $AutoClass = null;
113
        foreach ($classes as $class) {
114
            if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) {
115
                $AutoClass = $class;
116
            }
117
        }
118
119
        $Classes = $AutoClass::$classMap;
120
        return array_keys($Classes);
121
    }
122
}
123