Passed
Branch feature/cropping (c0151a)
by Philippe
02:30
created

AssetLibraryServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 16.98 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.62%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 10
c 4
b 2
f 0
lcom 1
cbo 4
dl 18
loc 106
ccs 41
cts 42
cp 0.9762
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 13 1
A registerEloquentFactoriesFrom() 0 4 1
A setupRoutes() 0 6 1
A register() 0 6 1
A registerModelBindings() 0 4 1
A registerAssetLibrary() 0 6 1
B publishMigrations() 18 23 4

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 Thinktomorrow\AssetLibrary;
4
5
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\ServiceProvider;
8
use Thinktomorrow\AssetLibrary\Models\Asset;
9
10
class AssetLibraryServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Indicates if loading of the provider is deferred.
14
     *
15
     * @var bool
16
     */
17
    protected $defer = false;
18
19
    /**
20
     * Perform post-registration booting of services.
21
     *
22
     * @return void
23
     */
24 53
    public function boot()
25
    {
26 53
        $this->publishes([
27 53
            __DIR__.'/../config/assetlibrary.php' => config_path('assetlibrary.php'),
28 53
        ], 'config');
29
30 53
        $this->setupRoutes($this->app->router);
0 ignored issues
show
Bug introduced by
Accessing router on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
31
32 53
        $this->publishMigrations();
33
34 53
        $this->registerModelBindings();
35 53
        $this->registerEloquentFactoriesFrom(__DIR__.'/../database/factories');
36 53
    }
37
38
    /**
39
     * Register factories.
40
     *
41
     * @param  string  $path
42
     * @return void
43
     */
44 53
    protected function registerEloquentFactoriesFrom($path)
45
    {
46 53
        $this->app->make(EloquentFactory::class)->load($path);
47 53
    }
48
49
    /**
50
     * Define the routes for the application.
51
     *
52
     * @param  \Illuminate\Routing\Router  $router
53
     * @return void
54
     */
55
    public function setupRoutes(Router $router)
56
    {
57 53
        $router->group(['namespace' => 'Thinktomorrow\AssetLibrary\Http\Controllers'], function ($router) {
58 53
            require __DIR__.'/Http/routes.php';
59 53
        });
60 53
    }
61
62
    /**
63
     * Register any package services.
64
     *
65
     * @return void
66
     */
67 53
    public function register()
68
    {
69 53
        $this->mergeConfigFrom(__DIR__.'/../config/assetlibrary.php', 'assetlibrary');
70
71 53
        $this->registerAssetLibrary();
72 53
    }
73
74 53
    protected function registerModelBindings()
75
    {
76
        //TODO implement this
77 53
    }
78
79
    /**
80
     *
81
     */
82
    private function registerAssetLibrary()
83
    {
84 53
        $this->app->singleton('asset', function ($app) {
85
            return new Asset($app);
86 53
        });
87 53
    }
88
89
    /**
90
     *
91
     */
92 53
    public function publishMigrations(): void
93
    {
94 53 View Code Duplication
        if (!class_exists('CreateAssetTable')) {
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...
95 1
            $this->publishes([
96 1
                __DIR__ . '/../database/migrations/create_asset_table.php' => database_path('migrations/' . date('Y_m_d_His',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
97 1
                        time()) . '_create_asset_table.php'),
98 1
            ], 'migrations');
99
100
        }
101
102 53 View Code Duplication
        if (!class_exists('CreateAssetPivotTable')) {
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...
103 1
            $this->publishes([
104 1
                __DIR__ . '/../database/migrations/create_asset_pivot_table.php' => database_path('migrations/' . date('Y_m_d_His',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
105 1
                        time()) . '_create_asset_pivot_table.php'),
106 1
            ], 'migrations');
107
        }
108
109 53 View Code Duplication
        if (! class_exists('CreateMediaTable')) {
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...
110 1
            $this->publishes([
111 1
                __DIR__.'/../../../spatie/laravel-medialibrary/database/migrations/create_media_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_media_table.php'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 196 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
112 1
            ], 'migrations');
113
        }
114 53
    }
115
}
116