Completed
Push — master ( 4eed4e...7b7e84 )
by Michael
01:53
created

ApplicationServiceProvider::getRouterService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer\Providers;
10
11
use Joomla\Application as JoomlaApplication;
12
use Joomla\Database\DatabaseDriver;
13
use Joomla\DI\{
14
	Container, ServiceProviderInterface
15
};
16
use Joomla\Input\{
17
	Cli, Input
18
};
19
use Joomla\StatsServer\{
20
	CliApplication, Console, Router, WebApplication
21
};
22
use Joomla\StatsServer\Commands as AppCommands;
23
use Joomla\StatsServer\Controllers\{
24
	DisplayControllerCreate, DisplayControllerGet, SubmitControllerCreate, SubmitControllerGet
25
};
26
use Joomla\StatsServer\Database\Migrations;
27
use Joomla\StatsServer\GitHub\GitHub;
28
use Joomla\StatsServer\Models\StatsModel;
29
use Joomla\StatsServer\Views\Stats\StatsJsonView;
30
use Psr\Log\LoggerInterface;
31
use TheIconic\Tracking\GoogleAnalytics\Analytics;
32
33
/**
34
 * Application service provider
35
 */
36
class ApplicationServiceProvider implements ServiceProviderInterface
37
{
38
	/**
39
	 * Registers the service provider with a DI container.
40
	 *
41
	 * @param   Container  $container  The DI container.
42
	 *
43
	 * @return  void
44
	 */
45 1
	public function register(Container $container)
46
	{
47
		/*
48
		 * Application Classes
49
		 */
50
51 1
		$container->alias(CliApplication::class, JoomlaApplication\AbstractCliApplication::class)
52 1
			->share(JoomlaApplication\AbstractCliApplication::class, [$this, 'getCliApplicationService'], true);
53
54 1
		$container->alias(WebApplication::class, JoomlaApplication\AbstractWebApplication::class)
55 1
			->share(JoomlaApplication\AbstractWebApplication::class, [$this, 'getWebApplicationService'], true);
56
57
		/*
58
		 * Application Class Dependencies
59
		 */
60
61 1
		$container->share(Analytics::class, [$this, 'getAnalyticsService'], true);
62 1
		$container->share(Cli::class, [$this, 'getInputCliService'], true);
63 1
		$container->share(Console::class, [$this, 'getConsoleService'], true);
64 1
		$container->share(Input::class, [$this, 'getInputService'], true);
65 1
		$container->share(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class, [$this, 'getColorProcessorService'], true);
66 1
		$container->share(JoomlaApplication\Cli\CliInput::class, [$this, 'getCliInputService'], true);
67 1
		$container->share(Router::class, [$this, 'getRouterService'], true);
68
69 1
		$container->alias(JoomlaApplication\Cli\CliOutput::class, JoomlaApplication\Cli\Output\Stdout::class)
70 1
			->share(JoomlaApplication\Cli\Output\Stdout::class, [$this, 'getCliOutputService'], true);
71
72
		/*
73
		 * Console Commands
74
		 */
75
76 1
		$container->share(AppCommands\HelpCommand::class, [$this, 'getHelpCommandService'], true);
77 1
		$container->share(AppCommands\InstallCommand::class, [$this, 'getInstallCommandService'], true);
78 1
		$container->share(AppCommands\Database\MigrateCommand::class, [$this, 'getDatabaseMigrateCommandService'], true);
79 1
		$container->share(AppCommands\Database\StatusCommand::class, [$this, 'getDatabaseStatusCommandService'], true);
80 1
		$container->share(AppCommands\Snapshot\RecentCommand::class, [$this, 'getSnapshotRecentCommandService'], true);
81 1
		$container->share(AppCommands\SnapshotCommand::class, [$this, 'getSnapshotCommandService'], true);
82 1
		$container->share(AppCommands\Tags\JoomlaCommand::class, [$this, 'getTagsJoomlaCommandService'], true);
83 1
		$container->share(AppCommands\Tags\PhpCommand::class, [$this, 'getTagsPhpCommandService'], true);
84 1
		$container->share(AppCommands\UpdateCommand::class, [$this, 'getUpdateCommandService'], true);
85
86
		/*
87
		 * MVC Layer
88
		 */
89
90
		// Controllers
91 1
		$container->share(DisplayControllerCreate::class, [$this, 'getDisplayControllerCreateService'], true);
92 1
		$container->share(DisplayControllerGet::class, [$this, 'getDisplayControllerGetService'], true);
93 1
		$container->share(SubmitControllerCreate::class, [$this, 'getSubmitControllerCreateService'], true);
94 1
		$container->share(SubmitControllerGet::class, [$this, 'getSubmitControllerGetService'], true);
95
96
		// Models
97 1
		$container->share(StatsModel::class, [$this, 'getStatsModelService'], true);
98
99
		// Views
100 1
		$container->share(StatsJsonView::class, [$this, 'getStatsJsonViewService'], true);
101 1
	}
102
103
	/**
104
	 * Get the Analytics class service
105
	 *
106
	 * @param   Container  $container  The DI container.
107
	 *
108
	 * @return  Analytics
109
	 */
110 1
	public function getAnalyticsService(Container $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container 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...
111
	{
112 1
		return new Analytics(true);
113
	}
114
115
	/**
116
	 * Get the CLI application service
117
	 *
118
	 * @param   Container  $container  The DI container.
119
	 *
120
	 * @return  CliApplication
121
	 */
122 1
	public function getCliApplicationService(Container $container) : CliApplication
123
	{
124 1
		$application = new CliApplication(
125 1
			$container->get(Cli::class),
126 1
			$container->get('config'),
127 1
			$container->get(JoomlaApplication\Cli\CliOutput::class),
128 1
			$container->get(JoomlaApplication\Cli\CliInput::class),
129 1
			$container->get(Console::class)
130
		);
131
132
		// Inject extra services
133 1
		$application->setLogger($container->get('monolog.logger.cli'));
134
135 1
		return $application;
136
	}
137
138
	/**
139
	 * Get the CliInput class service
140
	 *
141
	 * @param   Container  $container  The DI container.
142
	 *
143
	 * @return  JoomlaApplication\Cli\CliInput
144
	 */
145 1
	public function getCliInputService(Container $container) : JoomlaApplication\Cli\CliInput
0 ignored issues
show
Unused Code introduced by
The parameter $container 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...
146
	{
147 1
		return new JoomlaApplication\Cli\CliInput;
148
	}
149
150
	/**
151
	 * Get the CliOutput class service
152
	 *
153
	 * @param   Container  $container  The DI container.
154
	 *
155
	 * @return  JoomlaApplication\Cli\CliOutput
156
	 */
157 1
	public function getCliOutputService(Container $container) : JoomlaApplication\Cli\Output\Stdout
158
	{
159 1
		return new JoomlaApplication\Cli\Output\Stdout($container->get(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class));
160
	}
161
162
	/**
163
	 * Get the ColorProcessor class service
164
	 *
165
	 * @param   Container  $container  The DI container.
166
	 *
167
	 * @return  JoomlaApplication\Cli\Output\Processor\ColorProcessor
168
	 */
169 1
	public function getColorProcessorService(Container $container) : JoomlaApplication\Cli\Output\Processor\ColorProcessor
170
	{
171 1
		$processor = new JoomlaApplication\Cli\Output\Processor\ColorProcessor;
172
173
		/** @var Input $input */
174 1
		$input = $container->get(Cli::class);
175
176 1
		if ($input->getBool('nocolors', false))
177
		{
178
			$processor->noColors = true;
179
		}
180
181
		// Setup app colors (also required in "nocolors" mode - to strip them).
182 1
		$processor->addStyle('title', new JoomlaApplication\Cli\ColorStyle('yellow', '', ['bold']));
183
184 1
		return $processor;
185
	}
186
187
	/**
188
	 * Get the console service
189
	 *
190
	 * @param   Container  $container  The DI container.
191
	 *
192
	 * @return  Console
193
	 */
194 1
	public function getConsoleService(Container $container) : Console
195
	{
196 1
		$console = new Console;
197 1
		$console->setContainer($container);
198
199 1
		return $console;
200
	}
201
202
	/**
203
	 * Get the Database\MigrateCommand class service
204
	 *
205
	 * @param   Container  $container  The DI container.
206
	 *
207
	 * @return  AppCommands\Database\MigrateCommand
208
	 */
209 1
	public function getDatabaseMigrateCommandService(Container $container) : AppCommands\Database\MigrateCommand
210
	{
211 1
		$command = new AppCommands\Database\MigrateCommand($container->get(Migrations::class));
212
213 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
214 1
		$command->setInput($container->get(Input::class));
215 1
		$command->setLogger($container->get(LoggerInterface::class));
216
217 1
		return $command;
218
	}
219
220
	/**
221
	 * Get the Database\StatusCommand class service
222
	 *
223
	 * @param   Container  $container  The DI container.
224
	 *
225
	 * @return  AppCommands\Database\StatusCommand
226
	 */
227 1 View Code Duplication
	public function getDatabaseStatusCommandService(Container $container) : AppCommands\Database\StatusCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
228
	{
229 1
		$command = new AppCommands\Database\StatusCommand($container->get(Migrations::class));
230
231 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
232 1
		$command->setInput($container->get(Input::class));
233
234 1
		return $command;
235
	}
236
237
	/**
238
	 * Get the DisplayControllerCreate class service
239
	 *
240
	 * @param   Container  $container  The DI container.
241
	 *
242
	 * @return  DisplayControllerCreate
243
	 */
244 View Code Duplication
	public function getDisplayControllerCreateService(Container $container) : DisplayControllerCreate
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
245
	{
246
		$controller = new DisplayControllerCreate;
247
248
		$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
249
		$controller->setInput($container->get(Input::class));
250
251
		return $controller;
252
	}
253
254
	/**
255
	 * Get the DisplayControllerGet class service
256
	 *
257
	 * @param   Container  $container  The DI container.
258
	 *
259
	 * @return  DisplayControllerGet
260
	 */
261 1 View Code Duplication
	public function getDisplayControllerGetService(Container $container) : DisplayControllerGet
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
262
	{
263 1
		$controller = new DisplayControllerGet(
264 1
			$container->get(StatsJsonView::class)
265
		);
266
267 1
		$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
268 1
		$controller->setInput($container->get(Input::class));
269
270 1
		return $controller;
271
	}
272
273
	/**
274
	 * Get the HelpCommand class service
275
	 *
276
	 * @param   Container  $container  The DI container.
277
	 *
278
	 * @return  AppCommands\HelpCommand
279
	 */
280 1 View Code Duplication
	public function getHelpCommandService(Container $container) : AppCommands\HelpCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
281
	{
282 1
		$command = new AppCommands\HelpCommand;
283
284 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
285 1
		$command->setInput($container->get(Input::class));
286
287 1
		return $command;
288
	}
289
290
	/**
291
	 * Get the Input\Cli class service
292
	 *
293
	 * @param   Container  $container  The DI container.
294
	 *
295
	 * @return  Cli
296
	 */
297 1
	public function getInputCliService(Container $container) : Cli
0 ignored issues
show
Unused Code introduced by
The parameter $container 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...
298
	{
299 1
		return new Cli;
300
	}
301
302
	/**
303
	 * Get the Input class service
304
	 *
305
	 * @param   Container  $container  The DI container.
306
	 *
307
	 * @return  Input
308
	 */
309 1
	public function getInputService(Container $container) : Input
0 ignored issues
show
Unused Code introduced by
The parameter $container 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...
310
	{
311 1
		return new Input($_REQUEST);
312
	}
313
314
	/**
315
	 * Get the InstallCommand class service
316
	 *
317
	 * @param   Container  $container  The DI container.
318
	 *
319
	 * @return  AppCommands\InstallCommand
320
	 */
321 1 View Code Duplication
	public function getInstallCommandService(Container $container) : AppCommands\InstallCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
322
	{
323 1
		$command = new AppCommands\InstallCommand($container->get(DatabaseDriver::class));
324
325 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
326 1
		$command->setInput($container->get(Input::class));
327
328 1
		return $command;
329
	}
330
331
	/**
332
	 * Get the router service
333
	 *
334
	 * @param   Container  $container  The DI container.
335
	 *
336
	 * @return  Router
337
	 */
338 1
	public function getRouterService(Container $container) : Router
339
	{
340 1
		$router = (new Router($container->get(Input::class)))
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Router\Router::setControllerPrefix() has been deprecated with message: 2.0 Deprecated without replacement

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method Joomla\Router\Router::setDefaultController() has been deprecated with message: 2.0 Deprecated without replacement

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method Joomla\Router\Router::addMap() has been deprecated with message: 2.0 Deprecated without replacement

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
341 1
			->setControllerPrefix('Joomla\\StatsServer\\Controllers\\')
342 1
			->setDefaultController('DisplayController')
343 1
			->addMap('/submit', 'SubmitController')
344 1
			->addMap('/:source', 'DisplayController');
345
346 1
		$router->setContainer($container);
347
348 1
		return $router;
349
	}
350
351
	/**
352
	 * Get the SnapshotCommand class service
353
	 *
354
	 * @param   Container  $container  The DI container.
355
	 *
356
	 * @return  AppCommands\SnapshotCommand
357
	 */
358 1 View Code Duplication
	public function getSnapshotCommandService(Container $container) : AppCommands\SnapshotCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
359
	{
360 1
		$command = new AppCommands\SnapshotCommand($container->get(StatsJsonView::class));
361
362 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
363 1
		$command->setInput($container->get(Input::class));
364
365 1
		return $command;
366
	}
367
368
	/**
369
	 * Get the RecentCommand class service
370
	 *
371
	 * @param   Container  $container  The DI container.
372
	 *
373
	 * @return  AppCommands\Snapshot\RecentCommand
374
	 */
375 View Code Duplication
	public function getSnapshotRecentCommandService(Container $container) : AppCommands\Snapshot\RecentCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
376
	{
377
		$command = new AppCommands\Snapshot\RecentCommand($container->get(StatsJsonView::class));
378
379
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
380
		$command->setInput($container->get(Input::class));
381
382
		return $command;
383
	}
384
385
	/**
386
	 * Get the StatsJsonView class service
387
	 *
388
	 * @param   Container  $container  The DI container.
389
	 *
390
	 * @return  StatsJsonView
391
	 */
392 1
	public function getStatsJsonViewService(Container $container) : StatsJsonView
393
	{
394 1
		return new StatsJsonView(
395 1
			$container->get(StatsModel::class)
396
		);
397
	}
398
399
	/**
400
	 * Get the StatsModel class service
401
	 *
402
	 * @param   Container  $container  The DI container.
403
	 *
404
	 * @return  StatsModel
405
	 */
406 1
	public function getStatsModelService(Container $container) : StatsModel
407
	{
408 1
		return new StatsModel(
409 1
			$container->get(DatabaseDriver::class)
410
		);
411
	}
412
413
	/**
414
	 * Get the SubmitControllerCreate class service
415
	 *
416
	 * @param   Container  $container  The DI container.
417
	 *
418
	 * @return  SubmitControllerCreate
419
	 */
420 1 View Code Duplication
	public function getSubmitControllerCreateService(Container $container) : SubmitControllerCreate
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
421
	{
422 1
		$controller = new SubmitControllerCreate(
423 1
			$container->get(StatsModel::class)
424
		);
425
426 1
		$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
427 1
		$controller->setInput($container->get(Input::class));
428
429 1
		return $controller;
430
	}
431
432
	/**
433
	 * Get the SubmitControllerGet class service
434
	 *
435
	 * @param   Container  $container  The DI container.
436
	 *
437
	 * @return  SubmitControllerGet
438
	 */
439 1 View Code Duplication
	public function getSubmitControllerGetService(Container $container) : SubmitControllerGet
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
440
	{
441 1
		$controller = new SubmitControllerGet;
442
443 1
		$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
444 1
		$controller->setInput($container->get(Input::class));
445
446 1
		return $controller;
447
	}
448
449
	/**
450
	 * Get the Tags\JoomlaCommand class service
451
	 *
452
	 * @param   Container  $container  The DI container.
453
	 *
454
	 * @return  AppCommands\Tags\JoomlaCommand
455
	 */
456 1 View Code Duplication
	public function getTagsJoomlaCommandService(Container $container) : AppCommands\Tags\JoomlaCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
457
	{
458 1
		$command = new AppCommands\Tags\JoomlaCommand($container->get(GitHub::class));
459
460 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
461 1
		$command->setInput($container->get(Input::class));
462
463 1
		return $command;
464
	}
465
466
	/**
467
	 * Get the Tags\PhpCommand class service
468
	 *
469
	 * @param   Container  $container  The DI container.
470
	 *
471
	 * @return  AppCommands\Tags\PhpCommand
472
	 */
473 1 View Code Duplication
	public function getTagsPhpCommandService(Container $container) : AppCommands\Tags\PhpCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
474
	{
475 1
		$command = new AppCommands\Tags\PhpCommand($container->get(GitHub::class));
476
477 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
478 1
		$command->setInput($container->get(Input::class));
479
480 1
		return $command;
481
	}
482
483
	/**
484
	 * Get the UpdateCommand class service
485
	 *
486
	 * @param   Container  $container  The DI container.
487
	 *
488
	 * @return  AppCommands\UpdateCommand
489
	 */
490 1 View Code Duplication
	public function getUpdateCommandService(Container $container) : AppCommands\UpdateCommand
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
491
	{
492 1
		$command = new AppCommands\UpdateCommand;
493
494 1
		$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
495 1
		$command->setInput($container->get(Input::class));
496
497 1
		return $command;
498
	}
499
500
	/**
501
	 * Get the web application service
502
	 *
503
	 * @param   Container  $container  The DI container.
504
	 *
505
	 * @return  WebApplication
506
	 */
507 1
	public function getWebApplicationService(Container $container) : WebApplication
508
	{
509 1
		$application = new WebApplication($container->get(Input::class), $container->get('config'));
510
511
		// Inject extra services
512 1
		$application->setAnalytics($container->get(Analytics::class));
513 1
		$application->setLogger($container->get('monolog.logger.application'));
514 1
		$application->setRouter($container->get(Router::class));
515
516 1
		return $application;
517
	}
518
}
519