Test Failed
Push — master ( 269f87...2a7ff4 )
by Adam
01:43
created

DoctrineBlameableExtension::normalizeEntity()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 53.3673

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 2
cts 11
cp 0.1818
rs 8.0555
c 0
b 0
f 0
cc 9
nc 8
nop 1
crap 53.3673
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\Utils;
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
	 * @var array
40
	 */
41
	private $defaults = [
42
		'lazyAssociation' => FALSE,
43
		'userEntity'      => NULL,
44
		'automapField'    => TRUE,
45
		'userCallable'    => Security\UserCallable::class,
46
	];
47
48
	/**
49
	 * @return void
50
	 *
51
	 * @throws Utils\AssertionException
52
	 */
53
	public function loadConfiguration() : void
54
	{
55
		// Get container builder
56 1
		$builder = $this->getContainerBuilder();
57
58
		// Merge extension default config
59 1
		$this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters)));
60
61
		// Get extension configuration
62 1
		$configuration = $this->getConfig();
63
64 1
		Utils\Validators::assert($configuration['userEntity'], 'type|null', 'userEntity');
65 1
		Utils\Validators::assert($configuration['lazyAssociation'], 'bool', 'lazyAssociation');
66 1
		Utils\Validators::assert($configuration['automapField'], 'bool', 'automapField');
67
68 1
		$builder->addDefinition($this->prefix('configuration'))
69 1
			->setType(DoctrineBlameable\Configuration::class)
70 1
			->setArguments([
71 1
				$configuration['userEntity'],
72 1
				$configuration['lazyAssociation'],
73 1
				$configuration['automapField']
74
			]);
75
76 1
		$userCallable = NULL;
77
78 1
		if ($configuration['userCallable'] !== NULL) {
79 1
			$definition = $builder->addDefinition($this->prefix(md5($configuration['userCallable'])));
80
81 1
			list($factory) = DI\Helpers::filterArguments([
82 1
				is_string($configuration['userCallable']) ? new DI\Statement($configuration['userCallable']) : $configuration['userCallable']
83
			]);
84
85 1
			$definition->setFactory($factory);
86
87 1
			list($resolverClass) = (array) $this->normalizeEntity($definition->getFactory()->getEntity());
88
89
			if (class_exists($resolverClass)) {
90
				$definition->setType($resolverClass);
91
			}
92
93
			$userCallable = $definition;
94
		}
95
96
		$builder->addDefinition($this->prefix('driver'))
97
			->setType(Mapping\Driver\Blameable::class);
98
99
		$builder->addDefinition($this->prefix('subscriber'))
100
			->setType(Events\BlameableSubscriber::class)
101
			->setArguments([$userCallable instanceof DI\ServiceDefinition ? '@' . $userCallable->getType() : NULL]);
0 ignored issues
show
Bug introduced by
The class Nette\DI\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...
102
	}
103
104
	/**
105
	 * {@inheritdoc}
106
	 */
107
	public function beforeCompile() : void
108
	{
109
		parent::beforeCompile();
110
111
		$builder = $this->getContainerBuilder();
112
113
		$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface', TRUE))
114
			->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]);
115
	}
116
117
	/**
118
	 * @param Nette\Configurator $config
119
	 * @param string $extensionName
120
	 *
121
	 * @return void
122
	 */
123
	public static function register(Nette\Configurator $config, string $extensionName = 'doctrineBlameable') : void
124
	{
125 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
126 1
			$compiler->addExtension($extensionName, new DoctrineBlameableExtension);
127 1
		};
128 1
	}
129
130
	/**
131
	 * @return string|array  Class, @service, [Class, member], [@service, member], [, globalFunc], [Statement, member]
132
	 */
133
	private function normalizeEntity($entity)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
134
	{
135
		// Get container builder
136 1
		$builder = $this->getContainerBuilder();
137
138 1
		if (is_string($entity) && Strings::contains($entity, '::') && !Strings::contains($entity, '?')) { // Class::method -> [Class, method]
139
			$entity = explode('::', $entity);
140
		}
141
142
		if (is_array($entity) && $entity[0] instanceof ServiceDefinition) { // [ServiceDefinition, ...] -> [@serviceName, ...]
0 ignored issues
show
Bug introduced by
The class IPub\DoctrineBlameable\DI\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...
143
			$entity[0] = '@' . current(array_keys($builder->getDefinitions(), $entity[0], true));
144
145
		} elseif ($entity instanceof ServiceDefinition) { // ServiceDefinition -> @serviceName
0 ignored issues
show
Bug introduced by
The class IPub\DoctrineBlameable\DI\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...
146
			$entity = '@' . current(array_keys($builder->getDefinitions(), $entity, true));
147
148
		} elseif (is_array($entity) && $entity[0] === $builder) { // [$builder, ...] -> [@container, ...]
149
			trigger_error("Replace object ContainerBuilder in Statement entity with '@container'.", E_USER_DEPRECATED);
150
151
			$entity[0] = '@' . self::THIS_CONTAINER;
152
		}
153
154
		return $entity;
155
	}
156
}
157