Completed
Push — master ( ee0aed...da9ac8 )
by Michael
03:02
created

ApplicationServiceProvider   C

Complexity

Total Complexity 2

Size/Duplication

Total Lines 303
Duplicated Lines 28.38 %

Coupling/Cohesion

Components 0
Dependencies 24

Test Coverage

Coverage 49.43%

Importance

Changes 18
Bugs 2 Features 10
Metric Value
wmc 2
c 18
b 2
f 10
lcom 0
cbo 24
dl 86
loc 303
ccs 86
cts 174
cp 0.4943
rs 5.238

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 86 291 2

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
2
3
namespace Stats\Providers;
4
5
use Doctrine\Common\Cache\Cache;
6
use Joomla\Application as JoomlaApplication;
7
use Joomla\Database\DatabaseDriver;
8
use Joomla\DI\Container;
9
use Joomla\DI\ServiceProviderInterface;
10
use Joomla\Input\Cli;
11
use Joomla\Input\Input;
12
use Psr\Log\LoggerInterface;
13
use Stats\CliApplication;
14
use Stats\Commands\Cache\ClearCommand;
15
use Stats\Commands\Database\MigrateCommand;
16
use Stats\Commands\Database\StatusCommand;
17
use Stats\Commands\HelpCommand;
18
use Stats\Commands\InstallCommand;
19
use Stats\Commands\SnapshotCommand;
20
use Stats\Commands\UpdateCommand;
21
use Stats\Console;
22
use Stats\Controllers\DisplayControllerGet;
23
use Stats\Controllers\SubmitControllerCreate;
24
use Stats\Controllers\SubmitControllerGet;
25
use Stats\Database\Migrations;
26
use Stats\Models\StatsModel;
27
use Stats\Router;
28
use Stats\Views\Stats\StatsJsonView;
29
use Stats\WebApplication;
30
use TheIconic\Tracking\GoogleAnalytics\Analytics;
31
32
/**
33
 * Application service provider
34
 *
35
 * @since  1.0
36
 */
37
class ApplicationServiceProvider implements ServiceProviderInterface
38
{
39
	/**
40
	 * Registers the service provider with a DI container.
41
	 *
42
	 * @param   Container  $container  The DI container.
43
	 *
44
	 * @return  void
45
	 *
46
	 * @since   1.0
47
	 */
48 1
	public function register(Container $container)
0 ignored issues
show
Coding Style introduced by
register uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
49
	{
50 1
		$container->alias(CliApplication::class, JoomlaApplication\AbstractCliApplication::class)
51 1
			->share(
52 1
				JoomlaApplication\AbstractCliApplication::class,
53
				function (Container $container)
54
				{
55
					$application = new CliApplication(
56
						$container->get(Cli::class),
57
						$container->get('config'),
58
						$container->get(JoomlaApplication\Cli\CliOutput::class),
59
						$container->get(Console::class)
60
					);
61
62
					// Inject extra services
63
					$application->setContainer($container);
64
					$application->setLogger($container->get('monolog.logger.cli'));
65
66
					return $application;
67 1
				},
68
				true
69 1
			);
70
71 1
		$container->alias(WebApplication::class, JoomlaApplication\AbstractWebApplication::class)
72 1
			->share(
73 1
				JoomlaApplication\AbstractWebApplication::class,
74
				function (Container $container)
75
				{
76
					$application = new WebApplication($container->get(Input::class), $container->get('config'));
77
78
					// Inject extra services
79
					$application->setAnalytics($container->get(Analytics::class));
80
					$application->setLogger($container->get('monolog.logger.application'));
81
					$application->setRouter($container->get(Router::class));
82
83
					return $application;
84 1
				},
85
				true
86 1
			);
87
88 1
		$container->share(
89 1
			Input::class,
90
			function ()
91
			{
92
				return new Input($_REQUEST);
93 1
			},
94
			true
95 1
		);
96
97 1
		$container->share(
98 1
			Cli::class,
99
			function ()
100
			{
101
				return new Cli;
102 1
			},
103
			true
104 1
		);
105
106 1
		$container->share(
107 1
			Console::class,
108
			function (Container $container)
109
			{
110
				$console = new Console;
111
				$console->setContainer($container);
112
113
				return $console;
114
			}
115 1
		);
116
117 1
		$container->share(
118 1
			JoomlaApplication\Cli\Output\Processor\ColorProcessor::class,
119
			function (Container $container)
120
			{
121
				$processor = new JoomlaApplication\Cli\Output\Processor\ColorProcessor;
122
123
				/** @var Input $input */
124
				$input = $container->get(Cli::class);
125
126
				if ($input->get('nocolors'))
127
				{
128
					$processor->noColors = true;
129
				}
130
131
				// Setup app colors (also required in "nocolors" mode - to strip them).
132
				$processor->addStyle('title', new JoomlaApplication\Cli\ColorStyle('yellow', '', ['bold']));
133
134
				return $processor;
135
			}
136 1
		);
137
138 1
		$container->alias(JoomlaApplication\Cli\CliOutput::class, JoomlaApplication\Cli\Output\Stdout::class)
139 1
			->share(
140 1
				JoomlaApplication\Cli\Output\Stdout::class,
141
				function (Container $container)
142
				{
143
					return new JoomlaApplication\Cli\Output\Stdout($container->get(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class));
144
				}
145 1
			);
146
147 1
		$container->share(
148 1
			Analytics::class,
149
			function ()
150
			{
151
				return new Analytics(true);
152
			}
153 1
		);
154
155 1
		$container->share(
156 1
			Router::class,
157
			function (Container $container)
158
			{
159
				$router = (new Router($container->get(Input::class)))
160
					->setContainer($container)
161
					->setControllerPrefix('Stats\\Controllers\\')
162
					->setDefaultController('DisplayController')
163
					->addMap('/submit', 'SubmitController')
164
					->addMap('/:source', 'DisplayController');
165
166
				return $router;
167 1
			},
168
			true
169 1
		);
170
171 1
		$container->share(
172 1
			HelpCommand::class,
173 View Code Duplication
			function (Container $container)
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...
174
			{
175
				$command = new HelpCommand;
176
177
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
178
				$command->setInput($container->get(Input::class));
179
180
				return $command;
181 1
			},
182
			true
183 1
		);
184
185 1
		$container->share(
186 1
			InstallCommand::class,
187 View Code Duplication
			function (Container $container)
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...
188
			{
189
				$command = new InstallCommand($container->get(DatabaseDriver::class));
190
191
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
192
				$command->setInput($container->get(Input::class));
193
194
				return $command;
195 1
			},
196
			true
197 1
		);
198
199 1
		$container->share(
200 1
			SnapshotCommand::class,
201 View Code Duplication
			function (Container $container)
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...
202
			{
203
				$command = new SnapshotCommand($container->get(StatsJsonView::class));
204
205
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
206
				$command->setInput($container->get(Input::class));
207
208
				return $command;
209 1
			},
210
			true
211 1
		);
212
213 1
		$container->share(
214 1
			UpdateCommand::class,
215 View Code Duplication
			function (Container $container)
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...
216
			{
217
				$command = new UpdateCommand;
218
219
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
220
				$command->setInput($container->get(Input::class));
221
222
				return $command;
223 1
			},
224
			true
225 1
		);
226
227 1
		$container->share(
228 1
			ClearCommand::class,
229 View Code Duplication
			function (Container $container)
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...
230
			{
231
				$command = new ClearCommand($container->get(Cache::class));
232
233
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
234
				$command->setInput($container->get(Input::class));
235
236
				return $command;
237 1
			},
238
			true
239 1
		);
240
241 1
		$container->share(
242 1
			MigrateCommand::class,
243
			function (Container $container)
244
			{
245
				$command = new MigrateCommand($container->get(Migrations::class));
246
247
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
248
				$command->setInput($container->get(Input::class));
249
				$command->setLogger($container->get(LoggerInterface::class));
250
251
				return $command;
252 1
			},
253
			true
254 1
		);
255
256 1
		$container->share(
257 1
			StatusCommand::class,
258 View Code Duplication
			function (Container $container)
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...
259
			{
260
				$command = new StatusCommand($container->get(Migrations::class));
261
262
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
263
				$command->setInput($container->get(Input::class));
264
265
				return $command;
266 1
			},
267
			true
268 1
		);
269
270 1
		$container->share(
271 1
			DisplayControllerGet::class,
272 View Code Duplication
			function (Container $container)
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...
273
			{
274
				$controller = new DisplayControllerGet(
275
					$container->get(StatsJsonView::class),
276
					$container->get(Cache::class)
277
				);
278
279
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
280
				$controller->setInput($container->get(Input::class));
281
282
				return $controller;
283 1
			},
284
			true
285 1
		);
286
287 1
		$container->share(
288 1
			SubmitControllerCreate::class,
289 View Code Duplication
			function (Container $container)
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...
290
			{
291
				$controller = new SubmitControllerCreate(
292
					$container->get(StatsModel::class)
293
				);
294
295
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
296
				$controller->setInput($container->get(Input::class));
297
298
				return $controller;
299 1
			},
300
			true
301 1
		);
302
303 1
		$container->share(
304 1
			SubmitControllerGet::class,
305 View Code Duplication
			function (Container $container)
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...
306
			{
307
				$controller = new SubmitControllerGet;
308
309
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
310
				$controller->setInput($container->get(Input::class));
311
312
				return $controller;
313 1
			},
314
			true
315 1
		);
316
317 1
		$container->share(
318 1
			StatsModel::class,
319
			function (Container $container)
320
			{
321
				return new StatsModel(
322
					$container->get(DatabaseDriver::class)
323
				);
324 1
			},
325
			true
326 1
		);
327
328 1
		$container->share(
329 1
			StatsJsonView::class,
330 1
			function (Container $container)
331
			{
332
				return new StatsJsonView(
333
					$container->get(StatsModel::class)
334
				);
335 1
			},
336
			true
337 1
		);
338 1
	}
339
}
340