1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Managlea\Component; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ResourceMapper |
12
|
|
|
* @package Managlea\Component |
13
|
|
|
*/ |
14
|
|
|
final class ResourceMapper implements ResourceMapperInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $mapping; |
20
|
|
|
/** |
21
|
|
|
* @var |
22
|
|
|
*/ |
23
|
|
|
private $defaultEntityManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Initialize new ResourceMapper and set configs for usage |
27
|
|
|
*/ |
28
|
4 |
|
public function __construct() |
29
|
|
|
{ |
30
|
4 |
|
$conf = $this->setConfig(); |
31
|
4 |
|
$this->defaultEntityManager = $conf['default_entity_manager']; |
32
|
4 |
|
$this->mapping = $conf['mapping']; |
33
|
4 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $resourceName |
37
|
|
|
* @return mixed |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
3 |
|
private function getResourceMappingConf(string $resourceName) |
41
|
|
|
{ |
42
|
3 |
|
if (!array_key_exists($resourceName, $this->mapping)) { |
43
|
1 |
|
throw new \Exception(sprintf('Mapping configuration missing for resource: "%s"', $resourceName)); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return $this->mapping[$resourceName]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
4 |
|
private function setConfig() : array |
53
|
|
|
{ |
54
|
4 |
|
$configValues = Yaml::parse(file_get_contents(__DIR__ . '/../config/resource_mapping.yml')); |
55
|
|
|
|
56
|
4 |
|
return $configValues; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $resourceName |
61
|
|
|
* @return string |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
2 |
|
public function getEntityManagerName(string $resourceName) : string |
65
|
|
|
{ |
66
|
2 |
|
$resourceConf = $this->getResourceMappingConf($resourceName); |
67
|
1 |
|
if (is_array($resourceConf)) { |
68
|
1 |
|
return $resourceConf['entity_manager']; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $this->defaultEntityManager; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $resourceName |
76
|
|
|
* @return string |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
1 |
|
public function getObjectName(string $resourceName) : string |
80
|
|
|
{ |
81
|
1 |
|
$resourceConf = $this->getResourceMappingConf($resourceName); |
82
|
1 |
|
if (is_array($resourceConf)) { |
83
|
1 |
|
if (!array_key_exists('object_name', $resourceConf)) { |
84
|
1 |
|
$objectName = $resourceName; |
85
|
|
|
} else { |
86
|
1 |
|
$objectName = $resourceConf['object_name']; |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
1 |
|
$objectName = $resourceConf; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $objectName; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|