Completed
Push — develop ( c2f528...fd6fdd )
by Abdelrahman
01:09
created

ContactsServiceProvider::boot()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Contacts\Providers;
6
7
use Illuminate\Routing\Router;
8
use Illuminate\Support\ServiceProvider;
9
use Rinvex\Contacts\Contracts\ContactContract;
10
use Cortex\Contacts\Console\Commands\SeedCommand;
11
use Cortex\Contacts\Console\Commands\InstallCommand;
12
use Cortex\Contacts\Console\Commands\MigrateCommand;
13
use Cortex\Contacts\Console\Commands\PublishCommand;
14
use Illuminate\Database\Eloquent\Relations\Relation;
15
16
class ContactsServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * The commands to be registered.
20
     *
21
     * @var array
22
     */
23
    protected $commands = [
24
        MigrateCommand::class => 'command.cortex.contacts.migrate',
25
        PublishCommand::class => 'command.cortex.contacts.publish',
26
        InstallCommand::class => 'command.cortex.contacts.install',
27
        SeedCommand::class => 'command.cortex.contacts.seed',
28
    ];
29
30
    /**
31
     * Register any application services.
32
     *
33
     * This service provider is a great spot to register your various container
34
     * bindings with the application. As you can see, we are registering our
35
     * "Registrar" implementation here. You can add your own bindings too!
36
     *
37
     * @return void
38
     */
39
    public function register()
40
    {
41
        // Register console commands
42
        ! $this->app->runningInConsole() || $this->registerCommands();
43
    }
44
45
    /**
46
     * Bootstrap any application services.
47
     *
48
     * @return void
49
     */
50
    public function boot(Router $router)
51
    {
52
        // Bind route models and constrains
53
        $router->pattern('contact', '[a-z0-9-]+');
54
        $router->model('contact', ContactContract::class);
55
56
        // Map relations
57
        Relation::morphMap([
58
            'contact' => config('rinvex.contacts.models.contact'),
59
        ]);
60
61
        // Load resources
62
        require __DIR__.'/../../routes/breadcrumbs.php';
63
        $this->loadRoutesFrom(__DIR__.'/../../routes/web.php');
64
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/contacts');
65
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/contacts');
66
        ! $this->app->runningInConsole() || $this->loadMigrationsFrom(__DIR__.'/../../database/migrations');
67
        $this->app->afterResolving('blade.compiler', function () {
68
            require __DIR__.'/../../routes/menus.php';
69
        });
70
71
        // Publish Resources
72
        ! $this->app->runningInConsole() || $this->publishResources();
73
    }
74
75
    /**
76
     * Publish resources.
77
     *
78
     * @return void
79
     */
80
    protected function publishResources()
81
    {
82
        $this->publishes([realpath(__DIR__.'/../../resources/lang') => resource_path('lang/vendor/cortex/contacts')], 'cortex-contacts-lang');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
83
        $this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/cortex/contacts')], 'cortex-contacts-views');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 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...
84
    }
85
86
    /**
87
     * Register console commands.
88
     *
89
     * @return void
90
     */
91
    protected function registerCommands()
92
    {
93
        // Register artisan commands
94
        foreach ($this->commands as $key => $value) {
95
            $this->app->singleton($value, function ($app) use ($key) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
                return new $key();
97
            });
98
        }
99
100
        $this->commands(array_values($this->commands));
101
    }
102
}
103