Completed
Push — master ( 71c78b...6eeaca )
by recca
10:24 queued 05:45
created

TerminalServiceProvider::handleRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
ccs 8
cts 9
cp 0.8889
crap 2.0054
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Router;
8
use Illuminate\Support\ServiceProvider;
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, Recca0120\Terminal\ServiceProvider.

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 TerminalServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * namespace.
14
     *
15
     * @var string
16
     */
17
    protected $namespace = 'Recca0120\Terminal\Http\Controllers';
18
19
    /**
20
     * Bootstrap any application services.
21
     *
22
     * @param \Illuminate\Http\Request   $request
23
     * @param \Illuminate\Routing\Router $router
24
     */
25 1
    public function boot(Request $request, Router $router)
26
    {
27 1
        $config = $this->app['config']['terminal'];
28 1
        if (in_array($request->getClientIp(), Arr::get($config, 'whitelists', [])) === true || Arr::get($config, 'enabled') === true) {
29 1
            $this->loadViewsFrom(__DIR__.'/../resources/views', 'terminal');
30 1
            $this->handleRoutes($router, $config);
31 1
        }
32
33 1
        if ($this->app->runningInConsole() === true) {
1 ignored issue
show
Bug introduced by
The method runningInConsole() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34 1
            $this->handlePublishes();
35 1
        }
36 1
    }
37
38
    /**
39
     * Register any application services.
40
     */
41 1
    public function register()
42
    {
43 1
        $this->mergeConfigFrom(__DIR__.'/../config/terminal.php', 'terminal');
44
45
        $this->app->singleton(Application::class, function ($app) {
46 1
            $config = $app['config']['terminal'];
47 1
            $commands = $config['commands'];
48 1
            $artisan = new Application($app, $app['events'], $app->version());
49 1
            $artisan->resolveCommands($commands, true);
50
51 1
            return $artisan;
52 1
        });
53
54 1
        $this->app->singleton(Kernel::class, Kernel::class);
55
56
        $this->app->singleton(TerminalManager::class, function ($app) {
57 1
            $config = $app['config']['terminal'];
58
59 1
            return new TerminalManager($app->make(Kernel::class), array_merge($config, [
60 1
                'basePath' => $app->basePath(),
61 1
                'environment' => $app->environment(),
62 1
                'version' => $app->version(),
63 1
                'endpoint' => $app['url']->route(Arr::get($config, 'route.as').'endpoint'),
64 1
            ]));
65 1
        });
66 1
    }
67
68
    /**
69
     * register routes.
70
     *
71
     * @param \Illuminate\Routing\Router $router
72
     * @param array                     $config
73
     */
74 1
    protected function handleRoutes(Router $router, $config = [])
75
    {
76 1
        if ($this->app->routesAreCached() === false) {
1 ignored issue
show
Bug introduced by
The method routesAreCached() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77 1
            $router->group(array_merge([
78 1
                'namespace' => $this->namespace,
79 1
            ], Arr::get($config, 'route', [])), function (Router $router) {
80
                require __DIR__.'/Http/routes.php';
81 1
            });
82 1
        }
83 1
    }
84
85
    /**
86
     * handle publishes.
87
     */
88 1
    protected function handlePublishes()
89
    {
90 1
        $this->publishes([
91 1
            __DIR__.'/../config/terminal.php' => $this->app->configPath().'/terminal.php',
1 ignored issue
show
Bug introduced by
The method configPath() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92 1
        ], 'config');
93
94 1
        $this->publishes([
95 1
            __DIR__.'/../resources/views' => $this->app->basePath().'/resources/views/vendor/terminal',
96 1
        ], 'views');
97
98 1
        $this->publishes([
99 1
            __DIR__.'/../public' => $this->app['path.public'].'/vendor/terminal',
100 1
        ], 'public');
101 1
    }
102
}
103