Passed
Pull Request — 2.x (#729)
by Antonio Carlos
05:57
created

CapsulesServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 93
rs 10
c 1
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCapsules() 0 4 1
A registerManager() 0 5 1
A testCanMakeCapsule() 0 21 1
A registerViewPaths() 0 4 1
A registerCapsule() 0 3 1
A registerConfig() 0 9 1
A register() 0 3 1
A mergeTwillConfig() 0 12 1
A map() 0 6 1
1
<?php
2
3
namespace A17\Twill;
4
5
use Illuminate\Routing\Router;
6
use Illuminate\Support\Facades\Route;
7
use Illuminate\Support\ServiceProvider;
8
use A17\Twill\Services\Capsules\Manager;
9
use A17\Twill\Services\Routing\HasRoutes;
10
use A17\Twill\Services\Capsules\HasCapsules;
11
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, A17\Twill\RouteServiceProvider. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
class CapsulesServiceProvider extends RouteServiceProvider
14
{
15
    use HasRoutes, HasCapsules;
16
17
    protected $manager;
18
19
    protected function mergeTwillConfig()
20
    {
21
        $this->mergeConfigFrom(
22
            __DIR__ . '/../config/capsules.php',
23
            'twill.capsules'
24
        );
25
26
        $this->app
27
            ->make('config')
28
            ->set('twill.capsules.list', $this->getCapsuleList());
29
30
        $this->app->make('config')->set('twill.capsules.loaded', true);
31
    }
32
33
    public function register()
34
    {
35
        $this->registerConfig();
36
    }
37
38
    protected function registerConfig()
39
    {
40
        $this->registerManager();
41
42
        $this->mergeTwillConfig();
43
44
        $this->registerCapsules();
45
46
        $this->registerViewPaths();
47
    }
48
49
    public function registerCapsules()
50
    {
51
        $this->manager->getCapsuleList()->map(function ($capsule) {
52
            $this->registerCapsule($capsule);
53
        });
54
    }
55
56
    protected function registerCapsule($capsule)
57
    {
58
        $this->loadMigrationsFrom($capsule['migrations_dir']);
59
    }
60
61
    public function map(Router $router)
62
    {
63
        $this->manager
64
            ->getCapsuleList()
65
            ->each(function ($capsule) use ($router) {
66
                $this->registerCapsuleRoutes($router, $capsule);
67
            });
68
    }
69
70
    public function registerViewPaths()
71
    {
72
        $this->callAfterResolving('view', function ($view) {
73
            $view->addLocation(config('twill.capsules.path'));
74
        });
75
    }
76
77
    public function registerManager()
78
    {
79
        $this->app->instance(
80
            'twill.capsules.manager',
81
            $this->manager = new Manager()
82
        );
83
    }
84
85
    public function testCanMakeCapsule()
86
    {
87
        $this->assertExitCodeIsGood(
0 ignored issues
show
Bug introduced by
The method assertExitCodeIsGood() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        $this->/** @scrutinizer ignore-call */ 
88
               assertExitCodeIsGood(
Loading history...
88
            $this->artisan('twill:make:capsule', [
0 ignored issues
show
Bug introduced by
The method artisan() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            $this->/** @scrutinizer ignore-call */ 
89
                   artisan('twill:make:capsule', [
Loading history...
89
                'moduleName' => 'Cars',
90
                '--hasBlocks' => true,
91
                '--hasTranslation' => true,
92
                '--hasSlug' => true,
93
                '--hasMedias' => true,
94
                '--hasFiles' => true,
95
                '--hasPosition' => true,
96
                '--hasRevisions' => true,
97
            ])->run()
98
        );
99
100
        $this->assertFileExists(
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $this->/** @scrutinizer ignore-call */ 
101
               assertFileExists(
Loading history...
101
            twill_path('Twill/Capsules/Cars/app/Data/Models/Car.php')
102
        );
103
104
        $this->assertIsObject(
0 ignored issues
show
Bug introduced by
The method assertIsObject() does not exist on A17\Twill\CapsulesServiceProvider. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        $this->/** @scrutinizer ignore-call */ 
105
               assertIsObject(
Loading history...
105
            $this->app->make('App\Twill\Capsules\Cars\Data\Models\Car')
106
        );
107
    }
108
}
109