1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OneGuard DynamicConfigurationBundle. |
5
|
|
|
* |
6
|
|
|
* (c) OneGuard <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OneGuard\Bundle\DynamicConfigurationBundle; |
13
|
|
|
|
14
|
|
|
use OneGuard\Bundle\DynamicConfigurationBundle\Entity\ConfigurationValue; |
15
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
16
|
|
|
|
17
|
|
|
class ConfigurationResolver { |
18
|
|
|
/** |
19
|
|
|
* @var RegistryInterface |
20
|
|
|
*/ |
21
|
|
|
private $doctrine; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Definition |
25
|
|
|
*/ |
26
|
|
|
private $definition; |
27
|
|
|
|
28
|
|
|
public function __construct(RegistryInterface $doctrine, Definition $definition) { |
29
|
|
|
$this->doctrine = $doctrine; |
30
|
|
|
$this->definition = $definition; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getDefinition() : Definition { |
34
|
|
|
return $this->definition; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return null|object |
39
|
|
|
* @throws \UnexpectedValueException |
40
|
|
|
*/ |
41
|
|
|
public function resolve() { |
42
|
|
|
$repo = $this->doctrine->getRepository(ConfigurationValue::class); |
43
|
|
|
$configurationValue = $repo->find($this->definition->getKey()); |
44
|
|
|
if ($configurationValue === null) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
switch (get_class($this->definition)) { |
49
|
|
|
case StringDefinition::class: |
50
|
|
|
return $configurationValue->getValue(); |
51
|
|
|
case EntityDefinition::class: |
52
|
|
|
/* @var $entityDefinition EntityDefinition */ |
53
|
|
|
$entityDefinition = $this->definition; |
54
|
|
|
return $this->doctrine->getRepository($entityDefinition->getClass())->find($configurationValue->getValue()); |
55
|
|
|
default: |
56
|
|
|
throw new \UnexpectedValueException(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|