Completed
Push — master ( d2a193...a63b69 )
by Michael
02:24
created

ApplicationServiceProvider   C

Complexity

Total Complexity 2

Size/Duplication

Total Lines 261
Duplicated Lines 22.61 %

Coupling/Cohesion

Components 0
Dependencies 21

Test Coverage

Coverage 49.33%

Importance

Changes 15
Bugs 2 Features 7
Metric Value
wmc 2
c 15
b 2
f 7
lcom 0
cbo 21
dl 59
loc 261
ccs 74
cts 150
cp 0.4933
rs 6.1111

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 59 249 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\Database\MigrateCommand;
15
use Stats\Commands\Database\StatusCommand;
16
use Stats\Commands\HelpCommand;
17
use Stats\Commands\SnapshotCommand;
18
use Stats\Console;
19
use Stats\Controllers\DisplayControllerGet;
20
use Stats\Controllers\SubmitControllerCreate;
21
use Stats\Controllers\SubmitControllerGet;
22
use Stats\Database\Migrations;
23
use Stats\Models\StatsModel;
24
use Stats\Router;
25
use Stats\Views\Stats\StatsJsonView;
26
use Stats\WebApplication;
27
use TheIconic\Tracking\GoogleAnalytics\Analytics;
28
29
/**
30
 * Application service provider
31
 *
32
 * @since  1.0
33
 */
34
class ApplicationServiceProvider implements ServiceProviderInterface
35
{
36
	/**
37
	 * Registers the service provider with a DI container.
38
	 *
39
	 * @param   Container  $container  The DI container.
40
	 *
41
	 * @return  void
42
	 *
43
	 * @since   1.0
44
	 */
45 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...
46
	{
47 1
		$container->alias(CliApplication::class, JoomlaApplication\AbstractCliApplication::class)
48 1
			->share(
49 1
				JoomlaApplication\AbstractCliApplication::class,
50
				function (Container $container)
51
				{
52
					$application = new CliApplication(
53
						$container->get(Cli::class),
54
						$container->get('config'),
55
						$container->get(JoomlaApplication\Cli\CliOutput::class),
56
						$container->get(Console::class)
57
					);
58
59
					// Inject extra services
60
					$application->setContainer($container);
61
					$application->setLogger($container->get('monolog.logger.cli'));
62
63
					return $application;
64 1
				},
65
				true
66 1
			);
67
68 1
		$container->alias(WebApplication::class, JoomlaApplication\AbstractWebApplication::class)
69 1
			->share(
70 1
				JoomlaApplication\AbstractWebApplication::class,
71
				function (Container $container)
72
				{
73
					$application = new WebApplication($container->get(Input::class), $container->get('config'));
74
75
					// Inject extra services
76
					$application->setAnalytics($container->get(Analytics::class));
77
					$application->setLogger($container->get('monolog.logger.application'));
78
					$application->setRouter($container->get(Router::class));
79
80
					return $application;
81 1
				},
82
				true
83 1
			);
84
85 1
		$container->share(
86 1
			Input::class,
87
			function ()
88
			{
89
				return new Input($_REQUEST);
90 1
			},
91
			true
92 1
		);
93
94 1
		$container->share(
95 1
			Cli::class,
96
			function ()
97
			{
98
				return new Cli;
99 1
			},
100
			true
101 1
		);
102
103 1
		$container->share(
104 1
			Console::class,
105
			function (Container $container)
106
			{
107
				$console = new Console;
108
				$console->setContainer($container);
109
110
				return $console;
111
			}
112 1
		);
113
114 1
		$container->share(
115 1
			JoomlaApplication\Cli\Output\Processor\ColorProcessor::class,
116
			function (Container $container)
117
			{
118
				$processor = new JoomlaApplication\Cli\Output\Processor\ColorProcessor;
119
120
				/** @var Input $input */
121
				$input = $container->get(Cli::class);
122
123
				if ($input->get('nocolors'))
124
				{
125
					$processor->noColors = true;
126
				}
127
128
				// Setup app colors (also required in "nocolors" mode - to strip them).
129
				$processor->addStyle('title', new JoomlaApplication\Cli\ColorStyle('yellow', '', ['bold']));
130
131
				return $processor;
132
			}
133 1
		);
134
135 1
		$container->alias(JoomlaApplication\Cli\CliOutput::class, JoomlaApplication\Cli\Output\Stdout::class)
136 1
			->share(
137 1
				JoomlaApplication\Cli\Output\Stdout::class,
138
				function (Container $container)
139
				{
140
					return new JoomlaApplication\Cli\Output\Stdout($container->get(JoomlaApplication\Cli\Output\Processor\ColorProcessor::class));
141
				}
142 1
			);
143
144 1
		$container->share(
145 1
			Analytics::class,
146
			function ()
147
			{
148
				return new Analytics(true);
149
			}
150 1
		);
151
152 1
		$container->share(
153 1
			Router::class,
154
			function (Container $container)
155
			{
156
				$router = (new Router($container->get(Input::class)))
157
					->setContainer($container)
158
					->setControllerPrefix('Stats\\Controllers\\')
159
					->setDefaultController('DisplayController')
160
					->addMap('/submit', 'SubmitController')
161
					->addMap('/:source', 'DisplayController');
162
163
				return $router;
164 1
			},
165
			true
166 1
		);
167
168 1
		$container->share(
169 1
			HelpCommand::class,
170 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...
171
			{
172
				$command = new HelpCommand;
173
174
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
175
				$command->setInput($container->get(Input::class));
176
177
				return $command;
178 1
			},
179
			true
180 1
		);
181
182 1
		$container->share(
183 1
			SnapshotCommand::class,
184 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...
185
			{
186
				$command = new SnapshotCommand($container->get(StatsJsonView::class));
187
188
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
189
				$command->setInput($container->get(Input::class));
190
191
				return $command;
192 1
			},
193
			true
194 1
		);
195
196 1
		$container->share(
197 1
			MigrateCommand::class,
198
			function (Container $container)
199
			{
200
				$command = new MigrateCommand($container->get(Migrations::class));
201
202
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
203
				$command->setInput($container->get(Input::class));
204
				$command->setLogger($container->get(LoggerInterface::class));
205
206
				return $command;
207 1
			},
208
			true
209 1
		);
210
211 1
		$container->share(
212 1
			StatusCommand::class,
213 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...
214
			{
215
				$command = new StatusCommand($container->get(Migrations::class));
216
217
				$command->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
218
				$command->setInput($container->get(Input::class));
219
220
				return $command;
221 1
			},
222
			true
223 1
		);
224
225 1
		$container->share(
226 1
			DisplayControllerGet::class,
227 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...
228
			{
229
				$controller = new DisplayControllerGet(
230
					$container->get(StatsJsonView::class),
231
					$container->get(Cache::class)
232
				);
233
234
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
235
				$controller->setInput($container->get(Input::class));
236
237
				return $controller;
238 1
			},
239
			true
240 1
		);
241
242 1
		$container->share(
243 1
			SubmitControllerCreate::class,
244 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...
245
			{
246
				$controller = new SubmitControllerCreate(
247
					$container->get(StatsModel::class)
248
				);
249
250
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
251
				$controller->setInput($container->get(Input::class));
252
253
				return $controller;
254 1
			},
255
			true
256 1
		);
257
258 1
		$container->share(
259 1
			SubmitControllerGet::class,
260 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...
261
			{
262
				$controller = new SubmitControllerGet;
263
264
				$controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class));
265
				$controller->setInput($container->get(Input::class));
266
267
				return $controller;
268 1
			},
269
			true
270 1
		);
271
272 1
		$container->share(
273 1
			StatsModel::class,
274
			function (Container $container)
275
			{
276
				return new StatsModel(
277
					$container->get(DatabaseDriver::class)
278
				);
279 1
			},
280
			true
281 1
		);
282
283 1
		$container->share(
284 1
			StatsJsonView::class,
285 1
			function (Container $container)
286
			{
287
				return new StatsJsonView(
288
					$container->get(StatsModel::class)
289
				);
290 1
			},
291
			true
292 1
		);
293 1
	}
294
}
295