Passed
Push — master ( 7cfee1...18dfb5 )
by Alexander
07:21 queued 05:09
created

Container::addCommandFactories()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 63
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 5.7142

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
c 2
b 0
f 0
dl 0
loc 63
ccs 1
cts 41
cp 0.0244
rs 9.264
cc 2
nc 2
nop 0
crap 5.7142

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
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy;
12
13
14
use ConsoleHelpers\ConsoleKit\Config\ConfigEditor;
15
use ConsoleHelpers\DatabaseMigration\MigrationManager;
16
use ConsoleHelpers\DatabaseMigration\PhpMigrationRunner;
17
use ConsoleHelpers\DatabaseMigration\SqlMigrationRunner;
18
use ConsoleHelpers\SVNBuddy\Cache\CacheManager;
19
use ConsoleHelpers\SVNBuddy\Command\ChangelogCommand;
20
use ConsoleHelpers\SVNBuddy\Command\CleanupCommand;
21
use ConsoleHelpers\SVNBuddy\Command\CommitCommand;
22
use ConsoleHelpers\SVNBuddy\Command\CompletionCommand;
23
use ConsoleHelpers\SVNBuddy\Command\ConfigCommand;
24
use ConsoleHelpers\SVNBuddy\Command\ConflictsCommand;
25
use ConsoleHelpers\SVNBuddy\Command\Dev\MigrationCreateCommand;
26
use ConsoleHelpers\SVNBuddy\Command\Dev\PharCreateCommand;
27
use ConsoleHelpers\SVNBuddy\Command\LogCommand;
28
use ConsoleHelpers\SVNBuddy\Command\MergeCommand;
29
use ConsoleHelpers\SVNBuddy\Command\ProjectCommand;
30
use ConsoleHelpers\SVNBuddy\Command\ReparseCommand;
31
use ConsoleHelpers\SVNBuddy\Command\RevertCommand;
32
use ConsoleHelpers\SVNBuddy\Command\SearchCommand;
33
use ConsoleHelpers\SVNBuddy\Command\SelfUpdateCommand;
34
use ConsoleHelpers\SVNBuddy\Command\UpdateCommand;
35
use ConsoleHelpers\SVNBuddy\Config\CommandConfig;
36
use ConsoleHelpers\SVNBuddy\Database\StatementProfiler;
37
use ConsoleHelpers\SVNBuddy\Helper\DateHelper;
38
use ConsoleHelpers\SVNBuddy\Helper\OutputHelper;
39
use ConsoleHelpers\SVNBuddy\Helper\SizeHelper;
40
use ConsoleHelpers\SVNBuddy\MergeSourceDetector\ClassicMergeSourceDetector;
41
use ConsoleHelpers\SVNBuddy\MergeSourceDetector\InPortalMergeSourceDetector;
42
use ConsoleHelpers\SVNBuddy\MergeSourceDetector\MergeSourceDetectorAggregator;
43
use ConsoleHelpers\SVNBuddy\Process\ProcessFactory;
44
use ConsoleHelpers\SVNBuddy\Repository\CommitMessage\CommitMessageBuilder;
45
use ConsoleHelpers\SVNBuddy\Repository\CommitMessage\EmptyMergeTemplate;
46
use ConsoleHelpers\SVNBuddy\Repository\CommitMessage\GroupByBugMergeTemplate;
47
use ConsoleHelpers\SVNBuddy\Repository\CommitMessage\GroupByRevisionMergeTemplate;
48
use ConsoleHelpers\SVNBuddy\Repository\CommitMessage\MergeTemplateFactory;
49
use ConsoleHelpers\SVNBuddy\Repository\CommitMessage\SummaryMergeTemplate;
50
use ConsoleHelpers\SVNBuddy\Repository\Connector\CommandFactory;
51
use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector;
52
use ConsoleHelpers\SVNBuddy\Repository\Connector\UrlResolver;
53
use ConsoleHelpers\SVNBuddy\Repository\Parser\LogMessageParserFactory;
54
use ConsoleHelpers\SVNBuddy\Repository\Parser\RevisionListParser;
55
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\DatabaseManager;
56
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLogFactory;
57
use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionPrinter;
58
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyConflictTracker;
59
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyResolver;
60
use ConsoleHelpers\SVNBuddy\Updater\UpdateManager;
61
use ConsoleHelpers\SVNBuddy\Updater\Updater;
62
use ConsoleHelpers\SVNBuddy\Updater\VersionUpdateStrategy;
63
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
64
use Symfony\Component\Console\Exception\CommandNotFoundException;
65
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
66
use Symfony\Component\Console\Output\OutputInterface;
67
68
class Container extends \ConsoleHelpers\ConsoleKit\Container implements CommandLoaderInterface
69
{
70
71
	/**
72
	 * Command factories.
73
	 *
74
	 * @var callable[]
75
	 */
76
	protected $commandFactories = array();
77
78
	/**
79
	 * {@inheritdoc}
80
	 */
81 194
	public function __construct(array $values = array())
82
	{
83 194
		parent::__construct($values);
84
85 194
		$this['app_name'] = 'SVN-Buddy';
86 194
		$this['app_version'] = '@git-version@';
87
88 194
		$this['working_directory_sub_folder'] = '.svn-buddy';
89
90 194
		$this['config_defaults'] = array(
91
			'repository-connector.username' => '',
92
			'repository-connector.password' => '',
93
			'repository-connector.last-revision-cache-duration' => '10 minutes',
94
			'update-channel' => 'stable',
95
		);
96
97 194
		$this->extend('output', function ($output) {
98
			/** @var OutputInterface $output */
99 12
			$output->getFormatter()->setStyle('debug', new OutputFormatterStyle('white', 'magenta'));
100
101 12
			return $output;
102 194
		});
103
104 10
		$this['process_factory'] = function () {
105 10
			return new ProcessFactory();
106
		};
107
108 1
		$this['merge_source_detector'] = function () {
109 1
			$merge_source_detector = new MergeSourceDetectorAggregator(0);
110 1
			$merge_source_detector->add(new ClassicMergeSourceDetector(0));
111 1
			$merge_source_detector->add(new InPortalMergeSourceDetector(50));
112
113 1
			return $merge_source_detector;
114
		};
115
116 1
		$this['repository_url_resolver'] = function ($c) {
117 1
			return new UrlResolver($c['repository_connector']);
118
		};
119
120 10
		$this['cache_manager'] = function ($c) {
121 10
			return new CacheManager($c['working_directory'], $c['size_helper'], $c['io']);
122
		};
123
124 20
		$this['statement_profiler'] = function () {
125 20
			$statement_profiler = new StatementProfiler();
126
127
			// The "AbstractPlugin::getLastRevision" method.
128 20
			$statement_profiler->ignoreDuplicateStatement('SELECT LastRevision FROM PluginData WHERE Name = :name');
129
130
			// The "AbstractPlugin::getProject" method.
131 20
			$statement_profiler->ignoreDuplicateStatement('SELECT Id FROM Projects WHERE Path = :path');
132
133
			// The "AbstractDatabaseCollectorPlugin::getProjects" method.
134 20
			$statement_profiler->ignoreDuplicateStatement(
135 20
				'SELECT Path, Id AS PathId, RevisionAdded, RevisionDeleted, RevisionLastSeen
136
				FROM Paths
137
				WHERE PathHash IN (:path_hashes)'
138
			);
139
140
			// The "ProjectsPlugin::createRepositoryWideProject" method.
141 20
			$statement_profiler->ignoreDuplicateStatement(
142 20
				'SELECT Id FROM Paths WHERE ProjectPath = :project_path LIMIT 100'
143
			);
144
145 20
			$statement_profiler->setActive(true);
146 20
			$statement_profiler->trackDuplicates(false);
147
148 20
			return $statement_profiler;
149
		};
150
151 164
		$this['project_root_folder'] = function () {
152 164
			return dirname(dirname(__DIR__));
153
		};
154
155 164
		$this['migration_manager'] = function ($c) {
156 164
			$migrations_directory = $c['project_root_folder'] . '/migrations';
157 164
			$migration_manager = new MigrationManager($migrations_directory, $c);
158 164
			$migration_manager->registerMigrationRunner(new SqlMigrationRunner());
159 164
			$migration_manager->registerMigrationRunner(new PhpMigrationRunner());
160
161 164
			return $migration_manager;
162
		};
163
164 2
		$this['db_manager'] = function ($c) {
165 2
			return new DatabaseManager($c['working_directory'], $c['migration_manager'], $c['statement_profiler']);
166
		};
167
168 2
		$this['revision_log_factory'] = function ($c) {
169 2
			return new RevisionLogFactory(
170 2
				$c['repository_connector'],
171 2
				$c['db_manager'],
172 2
				$c['log_message_parser_factory']
173
			);
174
		};
175
176 3
		$this['log_message_parser_factory'] = function () {
177 3
			return new LogMessageParserFactory();
178
		};
179
180 9
		$this['revision_list_parser'] = function () {
181 9
			return new RevisionListParser();
182
		};
183
184 1
		$this['revision_printer'] = function ($c) {
185 1
			return new RevisionPrinter($c['date_helper'], $c['output_helper']);
186
		};
187
188 9
		$this['command_factory'] = function ($c) {
189 9
			return new CommandFactory($c['config_editor'], $c['process_factory'], $c['io'], $c['cache_manager']);
190
		};
191
192 8
		$this['repository_connector'] = function ($c) {
193 8
			return new Connector(
194 8
				$c['config_editor'],
195 8
				$c['command_factory'],
196 8
				$c['io'],
197 8
				$c['revision_list_parser']
198
			);
199
		};
200
201 1
		$this['commit_message_builder'] = function ($c) {
202 1
			return new CommitMessageBuilder($c['working_copy_conflict_tracker']);
203
		};
204
205 4
		$this['working_copy_resolver'] = function ($c) {
206 4
			return new WorkingCopyResolver($c['repository_connector']);
207
		};
208
209 2
		$this['working_copy_conflict_tracker'] = function ($c) {
210 2
			return new WorkingCopyConflictTracker($c['repository_connector'], $c['command_config']);
211
		};
212
213 1
		$this['merge_template_factory'] = function ($c) {
214 1
			$factory = new MergeTemplateFactory();
215 1
			$repository_connector = $c['repository_connector'];
216 1
			$revision_log_factory = $c['revision_log_factory'];
217
218 1
			$factory->add(new GroupByBugMergeTemplate($repository_connector, $revision_log_factory));
219 1
			$factory->add(new GroupByRevisionMergeTemplate($repository_connector, $revision_log_factory));
220 1
			$factory->add(new SummaryMergeTemplate($repository_connector, $revision_log_factory));
221 1
			$factory->add(new EmptyMergeTemplate($repository_connector, $revision_log_factory));
222
223 1
			return $factory;
224
		};
225
226 3
		$this['command_config'] = function ($c) {
227 3
			return new CommandConfig($c['config_editor'], $c['working_copy_resolver']);
228
		};
229
230 2
		$this['date_helper'] = function () {
231 2
			return new DateHelper();
232
		};
233
234 11
		$this['size_helper'] = function () {
235 11
			return new SizeHelper();
236
		};
237
238 2
		$this['output_helper'] = function () {
239 2
			return new OutputHelper();
240
		};
241
242 1
		$this['editor'] = function () {
243 1
			return new InteractiveEditor();
244
		};
245
246 1
		$this['updater'] = function ($c) {
247
			/** @var ConfigEditor $config_editor */
248 1
			$config_editor = $c['config_editor'];
249
250 1
			$update_strategy = new VersionUpdateStrategy();
251 1
			$update_strategy->setStability($config_editor->get('update-channel'));
0 ignored issues
show
Bug introduced by
It seems like $config_editor->get('update-channel') can also be of type array and array; however, parameter $stability of ConsoleHelpers\SVNBuddy\...trategy::setStability() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

251
			$update_strategy->setStability(/** @scrutinizer ignore-type */ $config_editor->get('update-channel'));
Loading history...
252 1
			$update_strategy->setCurrentLocalVersion($c['app_version']);
253
254 1
			$updater = new Updater(null, false);
255 1
			$updater->setStrategyObject($update_strategy);
256
257 1
			return $updater;
258
		};
259
260
		$this['update_manager'] = function ($c) {
261
			return new UpdateManager(
262
				$c['updater'],
263
				$c['config_editor'],
264
				$c['process_factory'],
265
				$c['working_directory']
266
			);
267
		};
268
269 194
		$this->addCommandFactories();
270 194
	}
271
272
	/**
273
	 * Adds command factories.
274
	 *
275
	 * @return void
276
	 */
277
	protected function addCommandFactories()
278
	{
279
		$this->commandFactories['merge'] = function () {
280
			return new MergeCommand();
281
		};
282
		$this->commandFactories['cleanup'] = function () {
283
			return new CleanupCommand();
284
		};
285
		$this->commandFactories['revert'] = function () {
286
			return new RevertCommand();
287
		};
288
		$this->commandFactories['reparse'] = function () {
289
			return new ReparseCommand();
290
		};
291
		$this->commandFactories['log'] = function () {
292
			return new LogCommand();
293
		};
294
		$this->commandFactories['update'] = function () {
295
			return new UpdateCommand();
296
		};
297
		$this->commandFactories['up'] = function () {
298
			return new UpdateCommand();
299
		};
300
		$this->commandFactories['commit'] = function () {
301
			return new CommitCommand();
302
		};
303
		$this->commandFactories['ci'] = function () {
304
			return new CommitCommand();
305
		};
306
		$this->commandFactories['conflicts'] = function () {
307
			return new ConflictsCommand();
308
		};
309
		$this->commandFactories['cf'] = function () {
310
			return new ConflictsCommand();
311
		};
312
		$this->commandFactories['config'] = function () {
313
			return new ConfigCommand();
314
		};
315
		$this->commandFactories['cfg'] = function () {
316
			return new ConfigCommand();
317
		};
318
		$this->commandFactories['project'] = function () {
319
			return new ProjectCommand();
320
		};
321
		$this->commandFactories['_completion'] = function () {
322
			return new CompletionCommand();
323
		};
324
		$this->commandFactories['search'] = function () {
325
			return new SearchCommand();
326
		};
327
		$this->commandFactories['self-update'] = function () {
328
			return new SelfUpdateCommand();
329
		};
330
		$this->commandFactories['changelog'] = function () {
331
			return new ChangelogCommand();
332
		};
333
334 194
		if ( strpos(__DIR__, 'phar://') === false ) {
335
			$this->commandFactories['dev:migration-create'] = function () {
336
				return new MigrationCreateCommand();
337
			};
338
			$this->commandFactories['dev:phar-create'] = function () {
339
				return new PharCreateCommand();
340
			};
341
		}
342 194
	}
343
344
	/**
345
	 * {@inheritdoc}
346
	 */
347
	public function has($name)
348
	{
349
		return isset($this->commandFactories[$name]);
350
	}
351
352
	/**
353
	 * {@inheritdoc}
354
	 *
355
	 * @throws CommandNotFoundException When command wasn't found.
356
	 */
357
	public function get($name)
358
	{
359
		if ( !isset($this->commandFactories[$name]) ) {
360
			throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
361
		}
362
363
		$factory = $this->commandFactories[$name];
364
365
		return $factory();
366
	}
367
368
	/**
369
	 * {@inheritdoc}
370
	 */
371
	public function getNames()
372
	{
373
		return array_keys($this->commandFactories);
374
	}
375
376
}
377