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

CapsulesServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 82.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 93
ccs 43
cts 52
cp 0.8269
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;
0 ignored issues
show
Bug introduced by
The trait A17\Twill\Services\Capsules\HasCapsules requires the property $command which is not provided by A17\Twill\CapsulesServiceProvider.
Loading history...
16
17
    protected $manager;
18
19 76
    protected function mergeTwillConfig()
20
    {
21 76
        $this->mergeConfigFrom(
22 76
            __DIR__ . '/../config/capsules.php',
23 76
            'twill.capsules'
24
        );
25
26 76
        $this->app
27 76
            ->make('config')
28 76
            ->set('twill.capsules.list', $this->getCapsuleList());
29
30 76
        $this->app->make('config')->set('twill.capsules.loaded', true);
31 76
    }
32
33 76
    public function register()
34
    {
35 76
        $this->registerConfig();
36 76
    }
37
38 76
    protected function registerConfig()
39
    {
40 76
        $this->registerManager();
41
42 76
        $this->mergeTwillConfig();
43
44 76
        $this->registerCapsules();
45
46 76
        $this->registerViewPaths();
47 76
    }
48
49 76
    public function registerCapsules()
50
    {
51 76
        $this->manager->getCapsuleList()->map(function ($capsule) {
52 7
            $this->registerCapsule($capsule);
53 76
        });
54 76
    }
55
56 7
    protected function registerCapsule($capsule)
57
    {
58 7
        $this->loadMigrationsFrom($capsule['migrations_dir']);
59 7
    }
60
61 76
    public function map(Router $router)
62
    {
63 76
        $this->manager
64 76
            ->getCapsuleList()
65 76
            ->each(function ($capsule) use ($router) {
66 7
                $this->registerCapsuleRoutes($router, $capsule);
67 76
            });
68 76
    }
69
70 76
    public function registerViewPaths()
71
    {
72 76
        $this->callAfterResolving('view', function ($view) {
73 76
            $view->addLocation(config('twill.capsules.path'));
74 76
        });
75 76
    }
76
77 76
    public function registerManager()
78
    {
79 76
        $this->app->instance(
80 76
            'twill.capsules.manager',
81 76
            $this->manager = new Manager()
82
        );
83 76
    }
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