Completed
Push — master ( 28aabc...f555d9 )
by Michael
03:32
created

ApplicationServiceProvider::register()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 220
Code Lines 124

Duplication

Lines 50
Ratio 22.73 %

Code Coverage

Tests 66
CRAP Score 2.5114

Importance

Changes 13
Bugs 2 Features 6
Metric Value
c 13
b 2
f 6
dl 50
loc 220
ccs 66
cts 133
cp 0.4962
rs 8.2857
cc 2
eloc 124
nc 1
nop 1
crap 2.5114

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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