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

ExtensionAdapterProxy::getEntityMappings()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\DI;
6
7
use Kdyby;
8
use Nette;
9
use SixtyEightPublishers;
10
11
final class ExtensionAdapterProxy implements
12
	IExtensionAdapter,
13
	Kdyby\Doctrine\DI\IEntityProvider,
14
	Kdyby\Doctrine\DI\ITargetEntityProvider,
15
	Kdyby\Translation\DI\ITranslationProvider
16
{
17
	use Nette\SmartObject;
18
19
	/** @var callable  */
20
	private $extensionAdapterFactory;
21
22
	/** @var \SixtyEightPublishers\User\DI\IExtensionAdapter  */
23
	private $extensionAdapter;
24
25
	/** @var bool  */
26
	private $propagationStopped = FALSE;
27
28
	/**
29
	 * @param callable $extensionAdapterFactory
30
	 */
31
	public function __construct(callable $extensionAdapterFactory)
32
	{
33
		$this->extensionAdapterFactory = $extensionAdapterFactory;
34
	}
35
36
	/**
37
	 * @param callable $cb
38
	 *
39
	 * @return mixed
40
	 */
41
	private function run(callable $cb)
42
	{
43
		if (TRUE === $this->propagationStopped) {
44
			return NULL;
45
		}
46
47
		try {
48
			return $cb();
49
		} catch (SixtyEightPublishers\User\Common\Exception\StopPropagationException $e) {
50
			$this->propagationStopped = TRUE;
51
		}
52
53
		return NULL;
54
	}
55
56
	/**
57
	 * @return \SixtyEightPublishers\User\DI\IExtensionAdapter
58
	 */
59
	private function getExtensionAdapter(): IExtensionAdapter
60
	{
61
		if (NULL === $this->extensionAdapter) {
62
			$factory = $this->extensionAdapterFactory;
63
			$this->extensionAdapter = $factory();
64
		}
65
66
		return $this->extensionAdapter;
67
	}
68
69
	/*************** interface \SixtyEightPublishers\User\DI\IExtensionAdapter ***************/
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	public static function getDefaults(): array
75
	{
76
		throw new SixtyEightPublishers\User\Common\Exception\RuntimeException(sprintf(
77
			'Can not call static method %s, object %s is just proxy.',
78
			__METHOD__,
79
			__CLASS__
80
		));
81
	}
82
83
	/**
84
	 * {@inheritdoc}
85
	 */
86
	public function loadConfiguration(): void
87
	{
88
		$this->run(function () {
89
			$this->getExtensionAdapter()->loadConfiguration();
90
		});
91
	}
92
93
	/**
94
	 * {@inheritdoc}
95
	 */
96
	public function beforeCompile(): void
97
	{
98
		$this->run(function () {
99
			$this->getExtensionAdapter()->beforeCompile();
100
		});
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function afterCompile(Nette\PhpGenerator\ClassType $class): void
107
	{
108
		$this->run(function () use ($class) {
109
			$this->getExtensionAdapter()->afterCompile($class);
110
		});
111
	}
112
113
	/**************** interface \Kdyby\Doctrine\DI\IEntityProvider ****************/
114
115
	/**
116
	 * {@inheritdoc}
117
	 */
118
	public function getEntityMappings(): array
119
	{
120
		return $this->run(function () {
121
			$adapter = $this->getExtensionAdapter();
122
123
			return $adapter instanceof Kdyby\Doctrine\DI\IEntityProvider ? $adapter->getEntityMappings() : [];
124
		}) ?? [];
125
	}
126
127
	/**************** interface \Kdyby\Doctrine\DI\ITargetEntityProvider ****************/
128
129
	/**
130
	 * {@inheritdoc}
131
	 */
132
	public function getTargetEntityMappings(): array
133
	{
134
		return $this->run(function () {
135
			$adapter = $this->getExtensionAdapter();
136
137
			return $adapter instanceof Kdyby\Doctrine\DI\ITargetEntityProvider ? $adapter->getTargetEntityMappings() : [];
138
		}) ?? [];
139
	}
140
141
	/**************** interface \Kdyby\Translation\DI\ITranslationProvider ****************/
142
143
	/**
144
	 * {@inheritdoc}
145
	 */
146
	public function getTranslationResources(): array
147
	{
148
		return $this->run(function () {
149
			$adapter = $this->getExtensionAdapter();
150
151
			return $adapter instanceof Kdyby\Translation\DI\ITranslationProvider ? $adapter->getTranslationResources() : [];
152
		}) ?? [];
153
	}
154
}
155