Passed
Push — master ( 517bd8...f7cc98 )
by Attila
06:55
created

ConcordServiceProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 88
c 0
b 0
f 0
rs 10

4 Methods

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