Passed
Push — master ( d943e6...a9e422 )
by Fabio
05:14
created

TSqlMapXmlConfiguration::resolveResultMapping()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 37.8525

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 21
ccs 2
cts 14
cp 0.1429
crap 37.8525
rs 8.8333
1
<?php
2
/**
3
 * TSqlMapXmlConfigBuilder, TSqlMapXmlConfiguration, TSqlMapXmlMappingConfiguration classes file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Data\SqlMap\Configuration
9
 */
10
11
namespace Prado\Data\SqlMap\Configuration;
12
13
use Prado\Data\SqlMap\TSqlMapManager;
14
use Prado\Data\SqlMap\DataMapper\TSqlMapConfigurationException;
15
16
/**
17
 * TSqlMapXmlConfig class.
18
 *
19
 * Configures the TSqlMapManager using xml configuration file.
20
 *
21
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
22
 * @package Prado\Data\SqlMap\Configuration
23
 * @since 3.1
24
 */
25
class TSqlMapXmlConfiguration extends TSqlMapXmlConfigBuilder
26
{
27
	/**
28
	 * @var TSqlMapManager manager
29
	 */
30
	private $_manager;
31
	/**
32
	 * @var string configuration file.
33
	 */
34
	private $_configFile;
35
	/**
36
	 * @var array global properties.
37
	 */
38
	private $_properties = [];
39
40
	/**
41
	 * @param TSqlMapManager $manager manager instance.
42 3
	 */
43
	public function __construct($manager)
44 3
	{
45 3
		$this->_manager = $manager;
46
	}
47 3
48
	public function getManager()
49 3
	{
50
		return $this->_manager;
51
	}
52
53
	protected function getConfigFile()
54
	{
55
		return $this->_configFile;
56
	}
57
58
	/**
59
	 * Configure the TSqlMapManager using the given xml file.
60
	 * @param string $filename SqlMap configuration xml file.
61 3
	 */
62
	public function configure($filename = null)
63 3
	{
64 3
		$this->_configFile = $filename;
65
		$document = $this->loadXmlDocument($filename, $this);
66 3
67
		foreach ($document->xpath('//property') as $property) {
68
			$this->loadGlobalProperty($property);
69
		}
70 3
71
		foreach ($document->xpath('//typeHandler') as $handler) {
72
			$this->loadTypeHandler($handler);
73
		}
74 3
75
		foreach ($document->xpath('//connection[last()]') as $conn) {
76
			$this->loadDatabaseConnection($conn);
77
		}
78
79 3
		//try to load configuration in the current config file.
80 3
		$mapping = new TSqlMapXmlMappingConfiguration($this);
81
		$mapping->configure($filename);
82 2
83 1
		foreach ($document->xpath('//sqlMap') as $sqlmap) {
84
			$this->loadSqlMappingFiles($sqlmap);
0 ignored issues
show
Bug introduced by
$sqlmap of type SimpleXMLElement is incompatible with the type array expected by parameter $node of Prado\Data\SqlMap\Config...::loadSqlMappingFiles(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
			$this->loadSqlMappingFiles(/** @scrutinizer ignore-type */ $sqlmap);
Loading history...
85
		}
86 2
87 2
		$this->resolveResultMapping();
88 2
		$this->attachCacheModels();
89
	}
90
91
	/**
92
	 * Load global replacement property.
93
	 * @param \SimpleXmlElement $node property node.
94
	 */
95
	protected function loadGlobalProperty($node)
96
	{
97
		$this->_properties[(string) $node['name']] = (string) $node['value'];
98
	}
99
100
	/**
101
	 * Load the type handler configurations.
102
	 * @param \SimpleXmlElement $node type handler node
103
	 */
104
	protected function loadTypeHandler($node)
105
	{
106
		$handler = $this->createObjectFromNode($node);
107
		$this->_manager->getTypeHandlers()->registerTypeHandler($handler);
108
	}
109
110
	/**
111
	 * Load the database connection tag.
112
	 * @param \SimpleXmlElement $node connection node.
113
	 */
114
	protected function loadDatabaseConnection($node)
115
	{
116
		$conn = $this->createObjectFromNode($node);
117
		$this->_manager->setDbConnection($conn);
118
	}
119
120
	/**
121
	 * Load SqlMap mapping configuration.
122
	 * @param array $node
123 1
	 */
124
	protected function loadSqlMappingFiles($node)
125 1
	{
126
		if (strlen($resource = (string) $node['resource']) > 0) {
127
			if (strpos($resource, '${') !== false) {
128
				$resource = $this->replaceProperties($resource);
129
			}
130
131
			$mapping = new TSqlMapXmlMappingConfiguration($this);
132
			$filename = $this->getAbsoluteFilePath($this->_configFile, $resource);
133
			$mapping->configure($filename);
134 1
		}
135
	}
136
137
	/**
138
	 * Resolve nest result mappings.
139 2
	 */
140
	protected function resolveResultMapping()
141 2
	{
142 2
		$maps = $this->_manager->getResultMaps();
143
		foreach ($maps as $entry) {
144
			foreach ($entry->getColumns() as $item) {
145
				$resultMap = $item->getResultMapping();
146
				if ($resultMap !== null && strlen($resultMap) > 0) {
147
					if ($maps->contains($resultMap)) {
148
						$item->setNestedResultMap($maps[$resultMap]);
149
					} else {
150
						throw new TSqlMapConfigurationException(
151
							'sqlmap_unable_to_find_result_mapping',
152
							$resultMap,
153
							$this->_configFile,
154
							$entry->getID()
155
						);
156
					}
157
				}
158
			}
159
			if ($entry->getDiscriminator() !== null) {
160
				$entry->getDiscriminator()->initialize($this->_manager);
161
			}
162 2
		}
163
	}
164
165
	/**
166
	 * Set the cache for each statement having a cache model property.
167 2
	 */
168
	protected function attachCacheModels()
169 2
	{
170 2
		foreach ($this->_manager->getMappedStatements() as $mappedStatement) {
171
			$model = $mappedStatement->getStatement()->getCacheModel();
172 2
			if ($model !== null && strlen($model) > 0) {
173
				$cache = $this->_manager->getCacheModel($model);
174
				$mappedStatement->getStatement()->setCache($cache);
175 2
			}
176
		}
177
	}
178
179
	/**
180
	 * Replace the place holders ${name} in text with properties the
181
	 * corresponding global property value.
182
	 * @param string $string original string.
183 3
	 * @return string string with global property replacement.
184
	 */
185 3
	public function replaceProperties($string)
186
	{
187
		foreach ($this->_properties as $find => $replace) {
188 3
			$string = str_replace('${' . $find . '}', $replace, $string);
189
		}
190
		return $string;
191
	}
192
}
193