Test Failed
Pull Request — master (#36)
by Alex
06:45
created

getCandidateControllers()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 5.3846
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 8
eloc 15
nc 18
nop 1
crap 8
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 MetadataBaseProvider
12
{
13
    /**
14
     * Bootstrap the application services.  Post-boot.
15
     *
16 68
     * @return void
17
     */
18 68
    public function boot()
19
    {
20 68
        $isCaching = true === $this->getIsCaching();
21
        $hasCache = null;
22
23
        if ($isCaching) {
24
            $hasCache = Cache::has('metadataControllers');
25
            if ($hasCache) {
26 68
                $meta = Cache::get('metadataControllers');
27
                App::instance('metadataControllers', $meta);
28 68
                return;
29 68
            }
30 68
        }
31 68
32 68
        $meta = App::make('metadataControllers');
33 68
34 68
        $Classes = $this->getClassMap();
35
        $ends = $this->getCandidateControllers($Classes);
36 68
37 68
        // now process each class that uses the metadata controller trait and stick results in $metamix
38 68
        $metamix = [];
39 68
        $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...
40
        foreach ($ends as $end) {
41 68
            $map = $end->getMappings();
42 68
            // verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for
43 68
            // a model can glom onto different controllers
44 68
            foreach ($map as $key => $lock) {
45 67
                if (!array_key_exists($key, $metamix)) {
46 67
                    // if we haven't yet got a mapping for this model, grab it holus-bolus
47 68
                    $metamix[$key] = $lock;
48
                    continue;
49
                }
50 68
                // if we do, make sure we aren't re-adding mappings for any of the CRUD verbs
51
                foreach ($lock as $barrel => $roll) {
52
                    assert(
53 68
                        !array_key_exists($barrel, $metamix[$key]),
54 68
                        'Mapping already defined for model '.$key.' and CRUD verb '.$barrel
55 67
                    );
56
                    $metamix[$key][$barrel] = $roll;
57
                }
58 67
            }
59 67
        }
60
61 67
        $meta->setMetadata($metamix);
62 67
63
        $key = 'metadataControllers';
64
        $this->handlePostBoot($isCaching, $hasCache, $key, $meta);
65
    }
66
67
    /**
68
     * Register the application services.  Boot-time only.
69
     *
70
     * @return void
71
     */
72
    public function register()
73 67
    {
74 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...
75
            return new MetadataControllerContainer();
76 68
        });
77
    }
78 68
79
    /**
80
     * @param $Classes
81
     * @return array
82
     * @throws \Exception
83
     */
84
    protected function getCandidateControllers($Classes)
85
    {
86
        $ends = [];
87 68
        $startName = defined('PODATA_LARAVEL_APP_ROOT_NAMESPACE') ? PODATA_LARAVEL_APP_ROOT_NAMESPACE : "App";
88 68
        foreach ($Classes as $name) {
89 68
            // not in app namespace, keep moving
90 68
            if (!\Illuminate\Support\Str::startsWith($name, $startName)) {
91
                continue;
92
            }
93
            // if class doesn't exist (for whatever reason), skip it now rather than muck about later
94
            if (!class_exists($name)) {
95
                continue;
96
            }
97
            try {
98
                if (in_array(MetadataControllerTrait::class, class_uses($name, false))) {
99
                    $ends[] = App::make($name);
100
                }
101
            } catch (\Exception $e) {
102
                if (!App::runningInConsole()) {
103
                    throw $e;
104
                }
105
                // Squash exceptions thrown here when running from CLI so app can continue booting
106
            }
107
        }
108
        return $ends;
109
    }
110
}
111