Passed
Push — master ( 7b5b50...9096f6 )
by Adam
01:59
created

DoctrineBlameableExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 88
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 44 5
A beforeCompile() 0 9 2
A register() 0 6 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\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 1
		$config = $this->getConfig($this->defaults);
56 1
		$builder = $this->getContainerBuilder();
57
58 1
		Utils\Validators::assert($config['userEntity'], 'type|null', 'userEntity');
59 1
		Utils\Validators::assert($config['lazyAssociation'], 'bool', 'lazyAssociation');
60 1
		Utils\Validators::assert($config['automapField'], 'bool', 'automapField');
61
62 1
		$builder->addDefinition($this->prefix('configuration'))
63 1
			->setType(DoctrineBlameable\Configuration::class)
64 1
			->setArguments([
65 1
				$config['userEntity'],
66 1
				$config['lazyAssociation'],
67 1
				$config['automapField']
68
			]);
69
70 1
		$userCallable = NULL;
71
72 1
		if ($config['userCallable'] !== NULL) {
73 1
			$definition = $builder->addDefinition($this->prefix(md5($config['userCallable'])));
74
75 1
			list($factory) = DI\Helpers::filterArguments([
76 1
				is_string($config['userCallable']) ? new DI\Statement($config['userCallable']) : $config['userCallable']
77
			]);
78
79 1
			$definition->setFactory($factory);
80
81 1
			list($resolverClass) = (array) $builder->normalizeEntity($definition->getFactory()->getEntity());
82
83 1
			if (class_exists($resolverClass)) {
84 1
				$definition->setType($resolverClass);
85
			}
86
87 1
			$userCallable = $definition;
88
		}
89
90 1
		$builder->addDefinition($this->prefix('driver'))
91 1
			->setType(Mapping\Driver\Blameable::class);
92
93 1
		$builder->addDefinition($this->prefix('subscriber'))
94 1
			->setType(Events\BlameableSubscriber::class)
95 1
			->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...
96 1
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101
	public function beforeCompile() : void
102
	{
103 1
		parent::beforeCompile();
104
105 1
		$builder = $this->getContainerBuilder();
106
107 1
		$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface') ?: 'doctrine.default.entityManager')
108 1
			->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]);
109 1
	}
110
111
	/**
112
	 * @param Nette\Configurator $config
113
	 * @param string $extensionName
114
	 *
115
	 * @return void
116
	 */
117
	public static function register(Nette\Configurator $config, string $extensionName = 'doctrineBlameable') : void
118
	{
119 1
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
120 1
			$compiler->addExtension($extensionName, new DoctrineBlameableExtension);
121 1
		};
122 1
	}
123
}
124