JsonAPIClientExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 54.84%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 87
ccs 17
cts 31
cp 0.5484
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 33 5
A register() 0 6 1
A normalizeTargetEntityMappings() 0 18 3
1
<?php
2
/**
3
 * JsonAPIClientExtension.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:JsonAPIClient!
9
 * @subpackage     DI
10
 * @since          1.0.0
11
 *
12
 * @date           05.05.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\JsonAPIClient\DI;
18
19
use Nette;
20
use Nette\DI;
21
use Nette\Utils;
22
23
use Neomerx\JsonApi;
24
25
use IPub\JsonAPIClient\Clients;
26
use IPub\JsonAPIClient\Schemas;
27
28
/**
29
 * API client extension container
30
 *
31
 * @package        iPublikuj:JsonAPIClient!
32
 * @subpackage     DI
33
 *
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36 1
final class JsonAPIClientExtension extends DI\CompilerExtension
37
{
38
	/**
39
	 * @var mixed[]
40
	 */
41
	private $defaults = [
42
		'baseUri' => NULL
43
	];
44
45
	/**
46
	 * @return void
47
	 *
48
	 * @throws Utils\AssertionException
49
	 */
50
	public function loadConfiguration() : void
51
	{
52 1
		$config = $this->getConfig($this->defaults);
53 1
		$builder = $this->getContainerBuilder();
54
55
		// Schemas collector
56 1
		$schemaProvider = $builder->addDefinition($this->prefix('schemas'))
57 1
			->setType(Schemas\SchemaProvider::class)
58 1
			->setFactory(Schemas\SchemaProvider::class);
59
60
		// HTTP client
61 1
		$builder->addDefinition($this->prefix('client'))
62 1
			->setType(Clients\GuzzleClient::class)
63 1
			->setArguments(['baseUri' => $config['baseUri']]);
64
65 1
		foreach ($this->compiler->getExtensions() as $extension) {
66 1
			if ($extension instanceof ITargetEntitySchemasProvider) {
67
				$targetMapping = $extension->getTargetEntitySchemaMappings();
68
69
				Utils\Validators::assert($targetMapping, 'array');
70
71
				$targetMapping = $this->normalizeTargetEntityMappings($targetMapping);
72
73
				foreach ($targetMapping as $entity => $schema) {
74
					if (strpos($schema, '@') === 1) {
75
						$schema = $builder->getByType(substr($schema, 1));
76
					}
77
78 1
					$schemaProvider->addSetup('?->addMapping(?, ?)', [$schemaProvider, $entity, $schema]);
79
				}
80
			}
81
		}
82 1
	}
83
84
	/**
85
	 * @param Nette\Configurator $config
86
	 * @param string $extensionName
87
	 *
88
	 * @return void
89
	 */
90
	public static function register(Nette\Configurator $config, $extensionName = 'jsonApiClient') : void
91
	{
92 1
		$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) : void {
93 1
			$compiler->addExtension($extensionName, new JsonAPIClientExtension);
94 1
		};
95 1
	}
96
97
	/**
98
	 * @param array $targetMapping
99
	 *
100
	 * @return mixed[]
101
	 *
102
	 * @throws Utils\AssertionException
103
	 */
104
	private function normalizeTargetEntityMappings(array $targetMapping) : array
105
	{
106
		$normalized = [];
107
108
		foreach ($targetMapping as $entity => $schema) {
109
			$entity = ltrim($entity, '\\');
110
111
			Utils\Validators::assert($schema, 'string|callable');
112
113
			if (!is_callable($schema)) {
114
				$schema = ltrim($schema, '\\');
115
			}
116
117
			$normalized[$entity] = $schema;
118
		}
119
120
		return $normalized;
121
	}
122
}
123