Completed
Pull Request — master (#3)
by Adam
05:05 queued 03:10
created

DoctrineBlameableExtension::getConfigSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * DoctrineBlameableExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:DoctrineBlameable!
9
 * @subpackage     DI
10
 * @since          1.0.0
11
 *
12
 * @date           01.01.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\DoctrineBlameable\DI;
18
19
use Nette;
20
use Nette\DI;
21
use Nette\Schema;
22
23
use IPub\DoctrineBlameable;
24
use IPub\DoctrineBlameable\Events;
25
use IPub\DoctrineBlameable\Mapping;
26
use IPub\DoctrineBlameable\Security;
27
28
/**
29
 * Doctrine blameable extension container
30
 *
31
 * @package        iPublikuj:DoctrineBlameable!
32
 * @subpackage     DI
33
 *
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36 1
final class DoctrineBlameableExtension extends DI\CompilerExtension
37
{
38
	/**
39
	 * {@inheritdoc}
40
	 */
41
	public function getConfigSchema() : Schema\Schema
42
	{
43 1
		return Schema\Expect::structure([
44 1
			'lazyAssociation' => Schema\Expect::bool(FALSE),
45 1
			'userEntity'      => Schema\Expect::anyOf(Schema\Expect::string(), Schema\Expect::type(DI\Definitions\Statement::class))->nullable(),
46 1
			'automapField'    => Schema\Expect::bool(TRUE),
47 1
			'userCallable'    => Schema\Expect::anyOf(Schema\Expect::string(), Schema\Expect::type(DI\Definitions\Statement::class))->default(Security\UserCallable::class),
48
		]);
49
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54
	public function loadConfiguration() : void
55
	{
56 1
		$builder = $this->getContainerBuilder();
57 1
		$configuration = $this->getConfig();
58
59 1
		$builder->addDefinition($this->prefix('configuration'))
60 1
			->setType(DoctrineBlameable\Configuration::class)
61 1
			->setArguments([
62 1
				$configuration->userEntity,
63 1
				$configuration->lazyAssociation,
64 1
				$configuration->automapField,
65
			]);
66
67 1
		$userCallable = NULL;
68
69 1
		if ($configuration->userCallable !== NULL) {
70 1
			$definition = $builder->addDefinition($this->prefix(md5($configuration->userCallable)));
71
72 1
			[$factory] = DI\Helpers::filterArguments([
0 ignored issues
show
Bug introduced by
The variable $factory does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73 1
				is_string($configuration->userCallable) ? new DI\Definitions\Statement($configuration->userCallable) : $configuration->userCallable,
74
			]);
75
76 1
			$definition->setFactory($factory);
77
78 1
			[$resolverClass] = (array) $this->normalizeEntity($definition->getFactory());
0 ignored issues
show
Bug introduced by
The variable $resolverClass does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
79
80
			if (class_exists($resolverClass)) {
81
				$definition->setType($resolverClass);
82
			}
83
84
			$userCallable = $definition;
85
		}
86
87
		$builder->addDefinition($this->prefix('driver'))
88
			->setType(Mapping\Driver\Blameable::class);
89
90
		$builder->addDefinition($this->prefix('subscriber'))
91
			->setType(Events\BlameableSubscriber::class)
92
			->setArguments([$userCallable instanceof DI\Definitions\ServiceDefinition ? '@' . $userCallable->getType() : NULL]);
0 ignored issues
show
Bug introduced by
The class Nette\DI\Definitions\ServiceDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
93
	}
94
95
	/**
96
	 * {@inheritdoc}
97
	 */
98
	public function beforeCompile() : void
99
	{
100
		parent::beforeCompile();
101
102
		$builder = $this->getContainerBuilder();
103
104
		$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface', TRUE))
105
			->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]);
106
	}
107
108
	/**
109
	 * @param Nette\Configurator $config
110
	 * @param string $extensionName
111
	 *
112
	 * @return void
113
	 */
114
	public static function register(
115
		Nette\Configurator $config,
116
		string $extensionName = 'doctrineBlameable'
117
	) : void {
118 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
119 1
			$compiler->addExtension($extensionName, new DoctrineBlameableExtension);
120 1
		};
121 1
	}
122
123
	/**
124
	 * @param $entity
125
	 *
126
	 * @return string|array  Class, @service, [Class, member], [@service, member], [, globalFunc], [Statement, member]
127
	 */
128
	private function normalizeEntity($entity)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
129
	{
130
		// Get container builder
131 1
		$builder = $this->getContainerBuilder();
132
133
		if (
134 1
			is_string($entity)
135 1
			&& Nette\Utils\Strings::contains($entity, '::')
136 1
			&& !Nette\Utils\Strings::contains($entity, '?')
137
		) { // Class::method -> [Class, method]
138
			$entity = explode('::', $entity);
139
		}
140
141
		if (
142 1
			is_array($entity)
143 1
			&& $entity[0] instanceof DI\Definitions\ServiceDefinition
0 ignored issues
show
Bug introduced by
The class Nette\DI\Definitions\ServiceDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
144
		) { // [ServiceDefinition, ...] -> [@serviceName, ...]
145
			$entity[0] = '@' . current(array_keys($builder->getDefinitions(), $entity[0], TRUE));
146
147
		} elseif (
148 1
			$entity instanceof DI\Definitions\ServiceDefinition
0 ignored issues
show
Bug introduced by
The class Nette\DI\Definitions\ServiceDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
149
		) { // ServiceDefinition -> @serviceName
150
			$entity = '@' . current(array_keys($builder->getDefinitions(), $entity, TRUE));
151
152
		} elseif (
153 1
			is_array($entity)
154 1
			&& $entity[0] === $builder
155
		) { // [$builder, ...] -> [@container, ...]
156
			trigger_error("Replace object ContainerBuilder in Statement entity with '@container'.", E_USER_DEPRECATED);
157
158
			$entity[0] = '@' . md5($entity);
159
		}
160
161 1
		return $entity;
162
	}
163
}
164