Completed
Push — develop ( 828103 )
by Jáchym
20:34
created

MigrationsExtension::loadConfiguration()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 17
Bugs 0 Features 2
Metric Value
c 17
b 0
f 2
dl 0
loc 41
rs 8.439
cc 5
eloc 26
nc 7
nop 0
1
<?php
2
3
/**
4
 * This file is part of Zenify
5
 * Copyright (c) 2014 Tomas Votruba (http://tomasvotruba.cz)
6
 */
7
8
namespace Zenify\DoctrineMigrations\DI;
9
10
use Arachne\EventDispatcher\DI\EventDispatcherExtension;
11
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand;
12
use Nette\DI\CompilerExtension;
13
use Symfony\Component\Console\Application;
14
use Symnedi\EventDispatcher\DI\EventDispatcherExtension as SymnediEventDispatcherExtension;
15
use Zenify\DoctrineMigrations\CodeStyle\CodeStyle;
16
use Zenify\DoctrineMigrations\Configuration\Configuration;
17
use Zenify\DoctrineMigrations\EventSubscriber\ChangeCodingStandardEventSubscriber;
18
use Zenify\DoctrineMigrations\EventSubscriber\RegisterMigrationsEventSubscriber;
19
use Zenify\DoctrineMigrations\EventSubscriber\SetConsoleOutputEventSubscriber;
20
use Zenify\DoctrineMigrations\Exception\DI\MissingExtensionException;
21
22
23
final class MigrationsExtension extends CompilerExtension
24
{
25
26
	/**
27
	 * @var string[]
28
	 */
29
	private $defaults = [
30
		'table' => 'doctrine_migrations',
31
		'column' => 'version',
32
		'directory' => '%appDir%/../migrations',
33
		'namespace' => 'Migrations',
34
		'codingStandard' => CodeStyle::INDENTATION_TABS,
35
		'versionsOrganization' => null,
36
	];
37
38
39
	private $subscribers = [
40
		ChangeCodingStandardEventSubscriber::class,
41
		RegisterMigrationsEventSubscriber::class,
42
		SetConsoleOutputEventSubscriber::class,
43
	];
44
45
46
	/**
47
	 * {@inheritdoc}
48
	 */
49
	public function loadConfiguration()
50
	{
51
		$containerBuilder = $this->getContainerBuilder();
52
53
		$this->compiler->parseServices(
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\Compiler::parseServices() has been deprecated.

This method has been deprecated.

Loading history...
54
			$containerBuilder,
55
			$this->loadFromFile(__DIR__ . '/../config/services.neon')
0 ignored issues
show
Bug introduced by
It seems like $this->loadFromFile(__DI.../config/services.neon') targeting Nette\DI\CompilerExtension::loadFromFile() can also be of type string; however, Nette\DI\Compiler::parseServices() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
		);
57
58
		if ($this->compiler->getExtensions(EventDispatcherExtension::class)) {
59
			$tag = EventDispatcherExtension::TAG_SUBSCRIBER;
60
		} elseif ($this->compiler->getExtensions(SymnediEventDispatcherExtension::class)) {
61
			$tag = NULL;
62
		} else {
63
			throw new MissingExtensionException(
64
				sprintf(
65
					'Please register required extension "%s" to your config. For now "%s" is also supported but is considered deprecated.',
66
					EventDispatcherExtension::class,
67
					SymnediEventDispatcherExtension::class
68
				)
69
			);
70
		}
71
72
		foreach ($this->subscribers as $key => $subscriber) {
73
			$definition = $containerBuilder
74
				->addDefinition($this->prefix('listener' . $key))
75
				->setClass($subscriber);
76
77
			if ($tag) {
78
				$definition->addTag($tag);
79
			}
80
		}
81
82
		$config = $this->getValidatedConfig();
83
84
		$containerBuilder->addDefinition($this->prefix('codeStyle'))
85
			->setClass(CodeStyle::class)
86
			->setArguments([$config['codingStandard']]);
87
88
		$this->addConfigurationDefinition($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->getValidatedConfig() on line 82 can also be of type string; however, Zenify\DoctrineMigration...nfigurationDefinition() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89
	}
90
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95
	public function beforeCompile()
96
	{
97
		$containerBuilder = $this->getContainerBuilder();
98
		$containerBuilder->prepareClassList();
99
100
		$this->setConfigurationToCommands();
101
		$this->loadCommandsToApplication();
102
	}
103
104
105
	private function addConfigurationDefinition(array $config)
106
	{
107
		$containerBuilder = $this->getContainerBuilder();
108
		$configurationDefinition = $containerBuilder->addDefinition($this->prefix('configuration'));
109
		$configurationDefinition
110
			->setClass(Configuration::class)
111
			->addSetup('setMigrationsTableName', [$config['table']])
112
			->addSetup('setMigrationsColumnName', [$config['column']])
113
			->addSetup('setMigrationsDirectory', [$config['directory']])
114
			->addSetup('setMigrationsNamespace', [$config['namespace']]);
115
116
		if ($config['versionsOrganization'] === Configuration::VERSIONS_ORGANIZATION_BY_YEAR) {
117
			$configurationDefinition->addSetup('setMigrationsAreOrganizedByYear');
118
		} elseif ($config['versionsOrganization'] === Configuration::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) {
119
			$configurationDefinition->addSetup('setMigrationsAreOrganizedByYearAndMonth');
120
		}
121
	}
122
123
124 View Code Duplication
	private function setConfigurationToCommands()
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...
125
	{
126
		$containerBuilder = $this->getContainerBuilder();
127
		$configurationDefinition = $containerBuilder->getDefinition($containerBuilder->getByType(Configuration::class));
128
129
		foreach ($containerBuilder->findByType(AbstractCommand::class) as $commandDefinition) {
130
			$commandDefinition->addSetup('setMigrationConfiguration', ['@' . $configurationDefinition->getClass()]);
131
		}
132
	}
133
134
135 View Code Duplication
	private function loadCommandsToApplication()
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...
136
	{
137
		$containerBuilder = $this->getContainerBuilder();
138
		$applicationDefinition = $containerBuilder->getDefinition($containerBuilder->getByType(Application::class));
139
		foreach ($containerBuilder->findByType(AbstractCommand::class) as $name => $commandDefinition) {
140
			$applicationDefinition->addSetup('add', ['@' . $name]);
141
		}
142
	}
143
144
145
	/**
146
	 * @return array
147
	 */
148
	private function getValidatedConfig()
149
	{
150
		$configuration = $this->getConfig($this->defaults);
151
		$this->validateConfig($configuration);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfig($this->defaults) on line 150 can also be of type string; however, Nette\DI\CompilerExtension::validateConfig() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
152
		$configuration['directory'] = $this->getContainerBuilder()->expand($configuration['directory']);
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ContainerBuilder::expand() has been deprecated.

This method has been deprecated.

Loading history...
153
154
		return $configuration;
155
	}
156
157
}
158