Completed
Pull Request — master (#12)
by Alex
03:28
created

MetadataControllerProvider::boot()   C

Complexity

Conditions 12
Paths 49

Size

Total Lines 58
Code Lines 32

Duplication

Lines 7
Ratio 12.07 %

Code Coverage

Tests 22
CRAP Score 27.55

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 58
ccs 22
cts 42
cp 0.5238
rs 6.5331
cc 12
eloc 32
nc 49
nop 0
crap 27.55

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
8
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer;
9
10
class MetadataControllerProvider extends ServiceProvider
11
{
12
    /**
13
     * Bootstrap the application services.  Post-boot.
14
     *
15
     * @return void
16
     */
17 67
    public function boot()
18
    {
19 67
        $isCaching = env('APP_METADATA_CACHING', false);
20
21 67
        if ($isCaching && Cache::has('metadataControllers')) {
22
            $meta = Cache::get('metadataControllers');
23
            $this->app->instance('metadataControllers', $meta);
24
            return;
25
        }
26
27 67
        $meta = $this->app->make('metadataControllers');
28
29 67
        $classes = get_declared_classes();
30 67
        $AutoClass = null;
31 67
        foreach ($classes as $class) {
32 67
            if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) {
33 67
                $AutoClass = $class;
34 67
            }
35 67
        }
36
37 67
        $metamix = [];
38 67
        $ends = array();
39 67
        $Classes = $AutoClass::$classMap;
40 67 View Code Duplication
        foreach ($Classes as $name => $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 67
            if (\Illuminate\Support\Str::startsWith($name, "App")) {
42
                if (in_array("AlgoWeb\\PODataLaravel\\Controllers\\MetadataControllerTrait", class_uses($name))) {
43
                    $ends[] = $name;
44
                }
45
            }
46 67
        }
47
48
        // now process each class that uses the metadata controller trait and stick results in $metamix
49 67
        $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...
50 67
        foreach ($ends as $end) {
51
            $map = $end->getMappings();
52
            // verify uniqueness - must be exactly one mapping for model-verb combo - different verb mappings for
53
            // a model can glom onto different controllers
54
            foreach ($map as $key => $lock) {
55
                if (!array_key_exists($key, $metamix)) {
56
                    // if we haven't yet got a mapping for this model, grab it holus-bolus
57
                    $metamix[$key] = $lock;
58
                    continue;
59
                }
60
                // if we do, make sure we aren't re-adding mappings for any of the CRUD verbs
61
                foreach ($lock as $barrel => $roll) {
62
                    assert(
63
                        !array_key_exists($barrel, $metamix[$key]),
64
                        'Mapping already defined for model '.$key. ' and CRUD verb '.$barrel
65
                    );
66
                    $metamix[$key][$barrel] = $roll;
67
                }
68
69
            }
70 67
        }
71
72 67
        $meta->setMetadata($metamix);
73
74 67
    }
75
76
    /**
77
     * Register the application services.  Boot-time only.
78
     *
79
     * @return void
80
     */
81
    public function register()
82
    {
83 67
        $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...
84 67
            return new MetadataControllerContainer();
85 67
        });
86 67
    }
87
}
88