Completed
Push — dev ( 7d09ec...9d3b21 )
by Marc
03:55 queued 01:43
created

ArtificerServiceProvider   B

Complexity

Total Complexity 25

Size/Duplication

Total Lines 205
Duplicated Lines 8.78 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 32
Bugs 6 Features 5
Metric Value
wmc 25
c 32
b 6
f 5
lcom 1
cbo 18
dl 18
loc 205
rs 7.3333

12 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 20 3
C manageCorePlugins() 0 32 7
A isBootable() 0 7 4
A requireFiles() 0 4 1
A getConfigPath() 0 3 1
A addPublishableFiles() 0 12 1
A addModel() 0 6 1
A addLocalization() 0 6 1
A addManagers() 18 22 1
A register() 0 19 3
A loadConfig() 0 7 1
A provides() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Mascame\Artificer;
2
3
use App;
4
use Illuminate\Support\ServiceProvider;
5
use Illuminate\Support\Str;
6
use Mascame\Artificer\Extension\Booter;
7
use Mascame\Artificer\Model\Model;
8
use Mascame\Artificer\Model\ModelObtainer;
9
use Mascame\Artificer\Model\ModelSchema;
10
use Mascame\Artificer\Widget\Manager as WidgetManager;
11
use Mascame\Artificer\Plugin\Manager as PluginManager;
12
use Mascame\Extender\Event\Event;
13
use Mascame\Extender\Installer\FileInstaller;
14
use Mascame\Extender\Installer\FileWriter;
15
16
17
class ArtificerServiceProvider extends ServiceProvider {
18
19
	use AutoPublishable, ServiceProviderLoader;
20
	
21
	protected $name = 'admin';
22
23
    protected $corePlugins = [
24
        \Mascame\Artificer\LoginPlugin::class
25
    ];
26
27
	/**
28
	 * Indicates if loading of the provider is deferred.
29
	 *
30
	 * @var bool
31
	 */
32
	protected $defer = false;
33
34
	/**
35
	 * @var bool
36
	 */
37
	protected $isBootable = false;
38
39
	/**
40
	 * Bootstrap the application events.
41
	 *
42
	 * @return void
43
	 */
44
	public function boot()
45
	{
46
		if (! $this->isBootable) return;
47
		
48
		$this->addPublishableFiles();
49
50
		// Wait until app is ready for config to be published
51
		if (! $this->isPublished()) return;
52
53
		$this->providers(config('admin.providers'));
54
		$this->aliases(config('admin.aliases'));
55
		$this->commands(config('admin.commands'));
56
57
        Artificer::pluginManager()->boot();
58
        Artificer::widgetManager()->boot();
59
60
        $this->manageCorePlugins();
61
62
        $this->requireFiles();
63
	}
64
65
    /**
66
     * Ensure core plugins are installed
67
     *
68
     * @throws \Exception
69
     */
70
	protected function manageCorePlugins() {
71
	    // Avoid installing plugins when using cli
72
        if (App::runningInConsole() || App::runningUnitTests()) return true;
73
74
        $pluginManager = Artificer::pluginManager();
75
        $needsRefresh = false;
76
77
        foreach ($this->corePlugins as $corePlugin) {
78
            if (! $pluginManager->isInstalled($corePlugin)) {
79
                $installed = $pluginManager->installer()->install($corePlugin);
80
81
                if (! $installed) {
82
                    throw new \Exception("Unable to install Artificer core plugin {$corePlugin}");
83
                }
84
85
                $needsRefresh = true;
86
            }
87
        }
88
89
        // Refresh to allow changes made by core plugins to take effect
90
        if ($needsRefresh) {
91
            /**
92
             * File driver is slow... wait some seconds (else we would have too many redirects)
93
             *
94
             * Fortunately we only do this in the first run. Ye, I don't like it either.
95
             */
96
            sleep(2);
97
98
            header('Location: '. \URL::current());
99
            die();
100
        }
101
    }
102
103
    /**
104
     * Determines if is on admin
105
     *
106
     * @return bool
107
     */
108
    public function isBootable($path, $routePrefix = null) {
109
        if (App::runningInConsole() || App::runningUnitTests()) return true;
110
111
        return (
112
            $path == $routePrefix || Str::startsWith($path, $routePrefix . '/')
113
        );
114
    }
115
116
	private function requireFiles()
117
	{
118
		require_once __DIR__ . '/../routes/admin.php';
119
	}
120
121
	protected function getConfigPath() {
122
		return config_path($this->name) . DIRECTORY_SEPARATOR;
123
	}
124
125
	private function addPublishableFiles()
126
    {
127
		$this->publishes([
128
			__DIR__.'/../resources/assets' => public_path('packages/mascame/' . $this->name),
129
		], 'public');
130
131
        $this->publishes([
132
            __DIR__.'/../config/' => $this->getConfigPath(),
133
        ], 'config');
134
135
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->name);
136
    }
137
138
	private function addModel()
139
	{
140
		App::singleton('ArtificerModel', function () {
141
			return new Model(new ModelSchema(new ModelObtainer()));
142
		});
143
	}
144
145
	private function addLocalization()
146
	{
147
		App::singleton('ArtificerLocalization', function () {
148
			return new Localization();
149
		});
150
	}
151
152
	private function addManagers()
153
	{
154 View Code Duplication
		App::singleton('ArtificerWidgetManager', function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            $widgetsConfig = $this->getConfigPath() . 'extensions/widgets.php';
156
157
            return new WidgetManager(
158
                new FileInstaller(new FileWriter(), $widgetsConfig),
159
                new Booter(),
160
                new Event(app('events'))
161
            );
162
        });
163
164 View Code Duplication
		App::singleton('ArtificerPluginManager', function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
            $pluginsConfig = $this->getConfigPath() . 'extensions/plugins.php';
166
167
            return new PluginManager(
168
                new FileInstaller(new FileWriter(), $pluginsConfig),
169
                new \Mascame\Artificer\Plugin\Booter(),
170
                new Event(app('events'))
171
            );
172
		});
173
	}
174
175
	/**
176
	 * Register the service provider.
177
	 *
178
	 * @return void
179
	 */
180
	public function register()
181
	{
182
		// We still haven't modified config, that's why 'admin.admin'
183
		$routePrefix = config('admin.admin.routePrefix');
184
185
		// Avoid bloating the App with files that will not be needed
186
		$this->isBootable = $this->isBootable(request()->path(), $routePrefix);
187
188
		if (! $this->isBootable) return;
189
190
		// We need the config published before we can use this package!
191
		if ($this->isPublished()) {
192
			$this->loadConfig();
193
194
			$this->addModel();
195
			$this->addLocalization();
196
			$this->addManagers();
197
		}
198
	}
199
200
	/**
201
	 * Moves admin/admin.php keys to the root level for commodity
202
	 */
203
	protected function loadConfig() {
204
		$config = config('admin');
205
		$config = ['admin' => array_merge($config, $config['admin'])];
206
		unset($config['admin']['admin']);
207
208
		config()->set($config);
209
	}
210
211
	/**
212
	 * Get the services provided by the provider.
213
	 *
214
	 * @return array
215
	 */
216
	public function provides()
217
	{
218
		return [];
219
	}
220
221
}
222