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

MetadataProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 111
Duplicated Lines 6.31 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 7.14%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 16
c 9
b 0
f 0
lcom 1
cbo 4
dl 7
loc 111
ccs 5
cts 70
cp 0.0714
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D boot() 7 80 14
A setupRoute() 0 8 1
A register() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Support\Facades\Cache;
7
use POData\Providers\Metadata\SimpleMetadataProvider;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\Facades\Schema as Schema;
10
11
class MetadataProvider extends ServiceProvider
12
{
13
    protected static $METANAMESPACE = "Data";
14
15
    /**
16
     * Bootstrap the application services.  Post-boot.
17
     *
18
     * @return void
19
     */
20 1
    public function boot()
21
    {
22 1
        self::$METANAMESPACE = env('ODataMetaNamespace', 'Data');
23
        // If we aren't migrated, there's no DB tables to pull metadata _from_, so bail out early
24
        try {
25 1
            if (!Schema::hasTable('migrations')) {
26
                return;
27
            }
28 1
        } catch (\Exception $e) {
29 1
            return;
30
        }
31
32
        self::setupRoute();
33
        $isCaching = env('APP_METADATA_CACHING', false);
34
35
        if ($isCaching && Cache::has('metadata')) {
36
            $meta = Cache::get('metadata');
37
            $this->app->instance('metadata', $meta);
38
            return;
39
        }
40
        $meta = $this->app->make('metadata');
41
42
        $classes = get_declared_classes();
43
        $AutoClass = null;
44
        foreach ($classes as $class) {
45
            if (\Illuminate\Support\Str::startsWith($class, "Composer\\Autoload\\ComposerStaticInit")) {
46
                $AutoClass = $class;
47
            }
48
        }
49
        $ends = array();
50
        $Classes = $AutoClass::$classMap;
51 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...
52
            if (\Illuminate\Support\Str::startsWith($name, "App")) {
53
                if (in_array("AlgoWeb\\PODataLaravel\\Models\\MetadataTrait", class_uses($name))) {
54
                    $ends[] = $name;
55
                }
56
            }
57
        }
58
59
        $EntityTypes = array();
60
        $ResourceSets = array();
61
        $begins = [];
62
        $numEnds = count($ends);
63
64
        for ($i = 0; $i < $numEnds; $i++) {
65
            $bitter = $ends[$i];
66
            $fqModelName = $bitter;
67
            $name = substr($bitter, strrpos($bitter, '\\')+1);
68
69
            $instance = new $fqModelName();
70
            $metaSchema = $instance->getXmlSchema();
71
            // if for whatever reason we don't get an XML schema, move on to next entry and drop current one from
72
            // further processing
73
            if (null == $metaSchema) {
74
                continue;
75
            }
76
            $EntityTypes[$fqModelName] = $metaSchema;
77
            $ResourceSets[$fqModelName] = $meta->addResourceSet(
78
                strtolower($name),
79
                $EntityTypes[$fqModelName]
80
            );
81
            $begins[] = $bitter;
82
        }
83
84
        $ends = $begins;
85
        unset($begins);
86
87
        // now that endpoints are hooked up, tackle the relationships
88
        // if we'd tried earlier, we'd be guaranteed to try to hook a relation up to null, which would be bad
89
        foreach ($ends as $bitter) {
90
            $fqModelName = $bitter;
91
            $instance = new $fqModelName();
92
            $instance->hookUpRelationships($EntityTypes, $ResourceSets);
93
        }
94
        if ($isCaching) {
95
            Cache::put('metadata', $meta, 10);
96
        } else {
97
            Cache::forget('metadata');
98
        }
99
    }
100
101
    private static function setupRoute()
102
    {
103
        $valueArray = [];
0 ignored issues
show
Unused Code introduced by
$valueArray 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...
104
105
        Route::any('odata.svc/{section}', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index')
106
            ->where(['section' => '.*']);
107
        Route::any('odata.svc', 'AlgoWeb\PODataLaravel\Controllers\ODataController@index');
108
    }
109
110
    /**
111
     * Register the application services.  Boot-time only.
112
     *
113
     * @return void
114
     */
115
    public function register()
116
    {
117
        $this->app->singleton('metadata', function ($app) {
0 ignored issues
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...
118
            return new SimpleMetadataProvider('Data', self::$METANAMESPACE);
119
        });
120
    }
121
}
122