Completed
Pull Request — master (#2)
by Tomáš
09:22
created

DoctrineIdentityExtensionAdapter::processConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\DoctrineIdentity\DI;
6
7
use Nette;
8
use SixtyEightPublishers;
9
10
final class DoctrineIdentityExtensionAdapter extends SixtyEightPublishers\User\DI\AbstractExtensionAdapter
11
{
12
	/** @var array  */
13
	protected static $defaults = [
14
		'enabled' => FALSE,
15
		'namespace' => NULL,
16
	];
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	protected function processConfig(array $config, \ArrayObject $sharedData): array
22
	{
23
		Nette\Utils\Validators::assertField($config, 'enabled', 'bool');
24
		Nette\Utils\Validators::assertField($config, 'namespace', 'null|string|' . Nette\DI\Statement::class);
25
		
26
		if (FALSE === $config['enabled']) {
27
			$this->stopPropagation();
28
		}
29
30
		return $config;
31
	}
32
33
	/**
34
	 * {@inheritdoc}
35
	 */
36
	public function loadConfiguration(): void
37
	{
38
		$this->getContainerBuilder()
39
			->addDefinition($this->prefix('user_storage'))
40
			->setType(Nette\Security\IUserStorage::class)
41
			->setFactory(SixtyEightPublishers\User\DoctrineIdentity\UserStorageProxy::class);
42
	}
43
44
	/**
45
	 * {@inheritdoc}
46
	 */
47
	public function beforeCompile(): void
48
	{
49
		$config = $this->getConfig();
50
		$builder = $this->getContainerBuilder();
51
		$userStorageProxy = $builder->getDefinition($this->prefix('user_storage'));
52
53
		foreach ($builder->findByType(Nette\Security\IUserStorage::class) as $name => $userStorage) {
54
			if ($name !== $this->prefix('user_storage') && TRUE === $userStorage->isAutowired()) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $name (integer) and $this->prefix('user_storage') (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
55
				break;
56
			}
57
		}
58
59
		if (!isset($userStorage)) {
60
			throw new SixtyEightPublishers\User\Common\Exception\RuntimeException(sprintf(
61
				'Autowired service of type %s not found.',
62
				Nette\Security\IUserStorage::class
63
			));
64
		}
65
66
		$userStorage->setAutowired(FALSE);
67
68
		$userStorageProxy->setAutowired(TRUE);
69
		$userStorageProxy->setArguments([
70
				'userStorage' => $userStorage,
71
		]);
72
		
73
		if (NULL !== $config['namespace']) {
74
			$userStorageProxy->addSetup('$service->setNamespace(?)', [
75
				'namespace' => $config['namespace'],
76
			]);
77
		}
78
	}
79
}
80