Passed
Push — master ( 49e198...27ddf9 )
by Alexander
16:42 queued 05:33
created

Container   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 303
Duplicated Lines 0 %

Test Coverage

Coverage 64.9%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
eloc 152
c 4
b 0
f 0
dl 0
loc 303
ccs 98
cts 151
cp 0.649
rs 10

5 Methods

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

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