Passed
Push — 2.x ( d066e3...5a086a )
by Quentin
11:25 queued 13s
created

CapsulesServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace A17\Twill;
4
5
use A17\Twill\Services\Capsules\HasCapsules;
6
use A17\Twill\Services\Capsules\Manager;
7
use A17\Twill\Services\Routing\HasRoutes;
8
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...
9
10
class CapsulesServiceProvider extends RouteServiceProvider
11
{
12
    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...
13
14
    protected $manager;
15
16
    protected function mergeTwillConfig()
17
    {
18
        $this->mergeConfigFrom(
19
            __DIR__ . '/../config/capsules.php',
20
            'twill.capsules'
21
        );
22
23
        $this->app
24
            ->make('config')
25
            ->set('twill.capsules.list', $this->getCapsuleList()->toArray());
26
27
        $this->app->make('config')->set('twill.capsules.loaded', true);
28
    }
29
30
    public function register()
31
    {
32
        $this->registerManager();
33
        $this->mergeTwillConfig();
34
    }
35
36
    public function boot()
37
    {
38
        $this->registerCapsules();
39
        $this->registerViewPaths();
40
    }
41
42
    public function registerCapsules()
43
    {
44
        $this->manager->getCapsuleList()->map(function ($capsule) {
45
            $this->registerCapsule($capsule);
46
        });
47
    }
48
49
    protected function registerCapsule($capsule)
50
    {
51
        $this->loadMigrationsFrom($capsule['migrations_dir']);
52
    }
53
54
    public function registerViewPaths()
55
    {
56
        if (file_exists(config('twill.capsules.path'))) {
57
            $callback = function ($view) {
58
                $view->addLocation(config('twill.capsules.path'));
59
            };
60
61
            $this->app->afterResolving('view', $callback);
62
63
            if ($this->app->resolved('view')) {
64
                $callback($this->app->make('view'), $this->app);
65
            }
66
        }
67
    }
68
69
    public function registerManager()
70
    {
71
        $this->app->instance(
72
            'twill.capsules.manager',
73
            $this->manager = new Manager()
74
        );
75
    }
76
77
    public function testCanMakeCapsule()
78
    {
79
        $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

79
        $this->/** @scrutinizer ignore-call */ 
80
               assertExitCodeIsGood(
Loading history...
80
            $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

80
            $this->/** @scrutinizer ignore-call */ 
81
                   artisan('twill:make:capsule', [
Loading history...
81
                'moduleName' => 'Cars',
82
                '--hasBlocks' => true,
83
                '--hasTranslation' => true,
84
                '--hasSlug' => true,
85
                '--hasMedias' => true,
86
                '--hasFiles' => true,
87
                '--hasPosition' => true,
88
                '--hasRevisions' => true,
89
            ])->run()
90
        );
91
92
        $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

92
        $this->/** @scrutinizer ignore-call */ 
93
               assertFileExists(
Loading history...
93
            twill_path('Twill/Capsules/Cars/app/Models/Car.php')
94
        );
95
96
        $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

96
        $this->/** @scrutinizer ignore-call */ 
97
               assertIsObject(
Loading history...
97
            $this->app->make('App\Twill\Capsules\Cars\Models\Car')
98
        );
99
    }
100
}
101