Completed
Push — master ( b51deb...a1caa1 )
by Adam
09:33
created

DoctrineBlameableExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 7
dl 0
loc 76
ccs 38
cts 38
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfiguration() 0 44 5
A beforeCompile() 0 7 2
A register() 0 6 1
1
<?php
2
/**
3
 * DoctrineBlameableExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://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
namespace IPub\DoctrineBlameable\DI;
16
17
use Nette;
18
use Nette\DI;
19
use Nette\Utils;
20
use Nette\PhpGenerator as Code;
21
22
use IPub\DoctrineBlameable;
23
use IPub\DoctrineBlameable\Events;
24
use IPub\DoctrineBlameable\Mapping;
25
use IPub\DoctrineBlameable\Security;
26
27
/**
28
 * Doctrine blameable extension container
29
 *
30
 * @package        iPublikuj:DoctrineBlameable!
31
 * @subpackage     DI
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35
final class DoctrineBlameableExtension extends DI\CompilerExtension
36
{
37
	/**
38
	 * @var array
39 1
	 */
40
	private $defaults = [
41
		'lazyAssociation' => FALSE,
42
		'userEntity'      => NULL,
43
		'automapField'    => TRUE,
44
		'userCallable'    => Security\UserCallable::CLASS_NAME,
45
	];
46
47
	public function loadConfiguration()
48
	{
49
		$config = $this->getConfig($this->defaults);
50
		$builder = $this->getContainerBuilder();
51
52 1
		Utils\Validators::assert($config['userEntity'], 'type|null', 'userEntity');
53 1
		Utils\Validators::assert($config['lazyAssociation'], 'bool', 'lazyAssociation');
54
		Utils\Validators::assert($config['automapField'], 'bool', 'automapField');
55 1
56 1
		$builder->addDefinition($this->prefix('configuration'))
57 1
			->setClass(DoctrineBlameable\Configuration::CLASS_NAME)
58
			->setArguments([
59 1
				$config['userEntity'],
60 1
				$config['lazyAssociation'],
61 1
				$config['automapField']
62 1
			]);
63 1
64 1
		$userCallable = NULL;
65 1
66
		if ($config['userCallable'] !== NULL) {
67 1
			$definition = $builder->addDefinition($this->prefix(md5($config['userCallable'])));
68
69 1
			list($factory) = DI\Compiler::filterArguments([
70 1
				is_string($config['userCallable']) ? new DI\Statement($config['userCallable']) : $config['userCallable']
71
			]);
72 1
73 1
			$definition->setFactory($factory);
74 1
75
			list($resolverClass) = (array) $builder->normalizeEntity($definition->getFactory()->getEntity());
76 1
77
			if (class_exists($resolverClass)) {
78 1
				$definition->setClass($resolverClass);
79
			}
80 1
81 1
			$userCallable = $definition;
82 1
		}
83
84 1
		$builder->addDefinition($this->prefix('driver'))
85 1
			->setClass(Mapping\Driver\Blameable::CLASS_NAME);
86
87 1
		$builder->addDefinition($this->prefix('subscriber'))
88 1
			->setClass(Events\BlameableSubscriber::CLASS_NAME)
89
			->setArguments([$userCallable instanceof DI\ServiceDefinition ? '@' . $userCallable->getClass() : NULL]);
90 1
	}
91 1
92 1
	public function beforeCompile()
93 1
	{
94 1
		$builder = $this->getContainerBuilder();
95
96
		$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface') ?: 'doctrine.default.entityManager')
97
			->addSetup('?->getEventManager()->addEventSubscriber(?)', ['@self', $builder->getDefinition($this->prefix('subscriber'))]);
98
	}
99
100
	/**
101
	 * @param Nette\Configurator $config
102 1
	 * @param string $extensionName
103 1
	 */
104 1
	public static function register(Nette\Configurator $config, $extensionName = 'doctrineBlameable')
105 1
	{
106
		$config->onCompile[] = function (Nette\Configurator $config, Nette\DI\Compiler $compiler) use ($extensionName) {
107 1
			$compiler->addExtension($extensionName, new DoctrineBlameableExtension);
108
		};
109
	}
110
}
111