Completed
Pull Request — master (#16)
by Alex
02:59
created

MetadataControllerProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 77.27%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 76
ccs 34
cts 44
cp 0.7727
rs 10

2 Methods

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