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

ExtensionAdapterProxy   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 144
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 14 3
A getExtensionAdapter() 0 9 2
A getDefaults() 0 8 1
A loadConfiguration() 0 6 1
A beforeCompile() 0 6 1
A afterCompile() 0 6 1
A getEntityMappings() 0 8 2
A getTargetEntityMappings() 0 8 2
A getTranslationResources() 0 8 2
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() : [];
0 ignored issues
show
Bug introduced by
The class Kdyby\Doctrine\DI\IEntityProvider 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...
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() : [];
0 ignored issues
show
Bug introduced by
The class Kdyby\Doctrine\DI\ITargetEntityProvider 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...
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() : [];
0 ignored issues
show
Bug introduced by
The class Kdyby\Translation\DI\ITranslationProvider 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...
152
		}) ?? [];
153
	}
154
}
155