ConcordServiceProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 11
eloc 33
c 5
b 2
f 0
dl 0
loc 82
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCommands() 0 10 2
A register() 0 31 5
A resolveConventionClass() 0 10 3
A boot() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Contains the ConcordServiceProvider class.
7
 *
8
 * @copyright   Copyright (c) 2016 Attila Fulop
9
 * @author      Attila Fulop
10
 * @license     MIT
11
 * @since       2016-08-14
12
 *
13
 */
14
15
namespace Konekt\Concord;
16
17
use Illuminate\Support\ServiceProvider;
18
use InvalidArgumentException;
19
use Konekt\Concord\Console\Commands\EnumsCommand;
20
use Konekt\Concord\Console\Commands\MakeModuleCommand;
21
use Konekt\Concord\Console\Commands\ModelsCommand;
22
use Konekt\Concord\Console\Commands\ModulesCommand;
23
use Konekt\Concord\Console\Commands\RequestsCommand;
24
use Konekt\Concord\Console\Commands\VersionCommand;
25
use Konekt\Concord\Contracts\Concord as ConcordContract;
26
use Konekt\Concord\Contracts\Convention;
27
use Konekt\Concord\Conventions\ConcordDefault;
28
29
class ConcordServiceProvider extends ServiceProvider
30
{
31
    public function register()
32
    {
33
        // Set convention to use
34
        $this->app->bind(
35
            Convention::class,
36
            $this->resolveConventionClass(
37
                $this->app->config->get('concord.convention', 'default')
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
38
            )
39
        );
40
41
        // Register interface -> actual class binding as singleton
42
        // For the sake of flexibility it's possible to replace
43
        // the actual class by setting `class` in the config
44
        $this->app->singleton(
45
            ConcordContract::class,
46
            $this->app->config->get('concord.class', Concord::class)
47
        );
48
49
        // Make an instance
50
        $concordInstance = $this->app->make(ConcordContract::class);
51
        // And make it available as the 'concord' service
52
        $this->app->instance('concord', $concordInstance);
53
54
        $this->registerCommands();
55
56
        $modules = config("concord.modules") ?: [];
57
58
        foreach ($modules as $key => $value) {
59
            $module = is_string($key) ? $key : $value;
60
            $config = is_array($value) ? $value : [];
61
            $concordInstance->registerModule($module, $config);
62
        }
63
    }
64
65
    /**
66
     * Post-registration booting
67
     *
68
     * @return void
69
     */
70
    public function boot()
71
    {
72
        $this->publishes([
73
            __DIR__ . '/../config/config.php' => config_path('concord.php')
74
        ], 'config');
75
    }
76
77
    /**
78
     * Register the console commands of this package
79
     */
80
    protected function registerCommands()
81
    {
82
        if ($this->app->runningInConsole()) {
83
            $this->commands([
84
                ModulesCommand::class,
85
                ModelsCommand::class,
86
                EnumsCommand::class,
87
                RequestsCommand::class,
88
                MakeModuleCommand::class,
89
                VersionCommand::class
90
            ]);
91
        }
92
    }
93
94
    /**
95
     * Resolve the convention class
96
     *
97
     * @param string $name Either a short name like 'default' or an FQCN
98
     *
99
     * @return string
100
     */
101
    private function resolveConventionClass(string $name)
102
    {
103
        if ('default' == $name) {
104
            return ConcordDefault::class;
105
        } elseif (class_exists($name)) {
106
            return $name;
107
        }
108
109
        throw new InvalidArgumentException(
110
            sprintf('%s is not a valid convention class', $name)
111
        );
112
    }
113
}
114