Test Failed
Push — master ( 6e983e...fa965e )
by Adam
05:49
created

DoctrineBlameableExtension::beforeCompile()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 2
nc 1
nop 0
crap 6
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]);
96 1
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101
	public function beforeCompile() : void
102
	{
103
		parent::beforeCompile();
104
105
		$builder = $this->getContainerBuilder();
106
107
		$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface') ?: 'doctrine.default.entityManager')
108
			->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]);
109
	}
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