Completed
Push — master ( 1328c6...ea8c07 )
by Alex
01:20
created

MetadataControllerProvider::boot()   C

Complexity

Conditions 12
Paths 61

Size

Total Lines 63
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 13.691

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 63
ccs 34
cts 44
cp 0.7727
rs 6.1308
cc 12
eloc 35
nc 61
nop 0
crap 13.691

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Support\Facades\Cache;
7
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer;
8
9
class MetadataControllerProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.  Post-boot.
13
     *
14
     * @return void
15
     */
16 68
    public function boot()
17
    {
18 68
        $isCaching = env('APP_METADATA_CACHING', false);
19
20 68
        if ($isCaching && Cache::has('metadataControllers')) {
21
            $meta = Cache::get('metadataControllers');
22
            $this->app->instance('metadataControllers', $meta);
23
            return;
24
        }
25
26 68
        $meta = $this->app->make('metadataControllers');
27
28 68
        $classes = get_declared_classes();
29 68
        $AutoClass = null;
30 68
        foreach ($classes as $class) {
31 68
            if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) {
32 68
                $AutoClass = $class;
33 68
            }
34 68
        }
35
36 68
        $metamix = [];
37 68
        $ends = array();
38 68
        $Classes = $AutoClass::$classMap;
39 68
        foreach ($Classes as $name => $file) {
40
            try {
41 68
                if (in_array(
42 68
                    "AlgoWeb\\PODataLaravel\\Controllers\\MetadataControllerTrait",
43 68
                    class_uses($name, false)
44 68
                )) {
45 67
                    $ends[] = new $name();
46 67
                }
47 68
            } catch (\Exception $e) {
48
                // Squash exceptions thrown here so app can continue booting
49
            }
50 68
        }
51
52
        // now process each class that uses the metadata controller trait and stick results in $metamix
53 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...
54 68
        foreach ($ends as $end) {
55 67
            $map = $end->getMappings();
56
            // verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for
57
            // a model can glom onto different controllers
58 67
            foreach ($map as $key => $lock) {
59 67
                if (!array_key_exists($key, $metamix)) {
60
                    // if we haven't yet got a mapping for this model, grab it holus-bolus
61 67
                    $metamix[$key] = $lock;
62 67
                    continue;
63
                }
64
                // if we do, make sure we aren't re-adding mappings for any of the CRUD verbs
65
                foreach ($lock as $barrel => $roll) {
66
                    assert(
67
                        !array_key_exists($barrel, $metamix[$key]),
68
                        'Mapping already defined for model '.$key.' and CRUD verb '.$barrel
69
                    );
70
                    $metamix[$key][$barrel] = $roll;
71
                }
72
73 67
            }
74 68
        }
75
76 68
        $meta->setMetadata($metamix);
77
78 68
    }
79
80
    /**
81
     * Register the application services.  Boot-time only.
82
     *
83
     * @return void
84
     */
85
    public function register()
86
    {
87 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...
88 68
            return new MetadataControllerContainer();
89 68
        });
90 68
    }
91
}
92