Completed
Push — master ( 2fd2f6...e7c3ef )
by Milan
01:35
created

Ares::createDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace h4kuna\Ares;
4
5
/**
6
 * @author Milan Matějček <[email protected]>
7
 */
8
class Ares
9
{
10
11
	const URL = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi';
12
13
	/** @var IFactory */
14
	private $factory;
15
16
	/** @var bool */
17
	private $activeMode;
18
19
	/** @var DataProvider */
20
	private $dataProvider;
21
22
23
	public function __construct(IFactory $factory = NULL)
24
	{
25
		if ($factory === NULL) {
26
			$factory = new Factory();
27
		}
28
		$this->factory = $factory;
29
	}
30
31
32
	/**
33
	 * Load fresh data.
34
	 * @param int|string $in
35
	 * @return Data
36
	 * @throws IdentificationNumberNotFoundException
37
	 */
38
	public function loadData($in)
39
	{
40
		try {
41
			$this->loadXML((string) $in, TRUE);
42
		} catch (IdentificationNumberNotFoundException $e) {
43
			$this->loadXML((string) $in, FALSE);
44
		}
45
		return $this->getData();
46
	}
47
48
49
	/**
50
	 * Get temporary data.
51
	 * @return Data
52
	 */
53
	public function getData()
54
	{
55
		return $this->getDataProvider()->getData();
56
	}
57
58
59
	/**
60
	 * Load XML and fill Data object
61
	 * @param string $in
62
	 * @param bool $activeOnly
63
	 * @throws IdentificationNumberNotFoundException
64
	 */
65
	private function loadXML($in, $activeOnly)
66
	{
67
		$client = $this->factory->createGuzzleClient();
68
		$xmlSource = $client->request('GET', $this->createUrl($in, $activeOnly))->getBody();
69
		$xml = @simplexml_load_string($xmlSource);
70
		if (!$xml) {
71
			throw new IdentificationNumberNotFoundException($in);
72
		}
73
74
		$ns = $xml->getDocNamespaces();
75
		$xmlEl = $xml->children($ns['are'])->children($ns['D'])->VBAS;
76
77
		if (!isset($xmlEl->ICO)) {
78
			throw new IdentificationNumberNotFoundException($in);
79
		}
80
81
		$this->processXml($xmlEl, $this->getDataProvider()->prepareData());
82
	}
83
84
85
	protected function processXml($xml, DataProvider $dataProvider)
86
	{
87
		$dataProvider->setIN($xml->ICO)
88
			->setTIN($xml->DIC)
89
			->setCompany($xml->OF)
90
			->setZip(self::exists($xml->AA, 'PSC'))
91
			->setStreet(self::exists($xml->AA, 'NU'))
92
			->setCity(self::exists($xml->AA, 'N'))
93
			->setHouseNumber(self::exists($xml->AA, 'CD'), self::exists($xml->AA, 'CO'))
94
			->setCityPost(self::exists($xml->AA, 'NMC'))
95
			->setCityDistrict(self::exists($xml->AA, 'NCO'))
96
			->setIsPerson(self::exists($xml->PF, 'KPF'))
97
			->setCreated($xml->DV);
98
99
		if (isset($xml->ROR)) {
100
			$dataProvider->setActive($xml->ROR->SOR->SSU)
101
				->setFileNumber($xml->ROR->SZ->OV)
102
				->setCourt($xml->ROR->SZ->SD->T);
103
		} else {
104
			$dataProvider->setActive($this->activeMode)
105
				->setFileNumber('')
106
				->setCourt('');
107
		}
108
		if (!$this->isActiveMode()) {
109
			$dataProvider->setActive('no');
110
		}
111
	}
112
113
114
	protected function isActiveMode()
115
	{
116
		return $this->activeMode === TRUE;
117
	}
118
119
120
	private function createUrl($inn, $activeOnly)
121
	{
122
		$this->activeMode = (bool) $activeOnly;
123
		$parameters = [
124
			'ico' => $inn,
125
			'aktivni' => $activeOnly ? 'true' : 'false',
126
		];
127
		return self::URL . '?' . http_build_query($parameters);
128
	}
129
130
131
	/**
132
	 * @return DataProvider
133
	 */
134
	private function getDataProvider()
135
	{
136
		if ($this->dataProvider === NULL) {
137
			$this->dataProvider = $this->factory->createDataProvider();
138
		}
139
		return $this->dataProvider;
140
	}
141
142
143
	private static function exists($element, $property)
144
	{
145
		return isset($element->{$property}) ? $element->{$property} : '';
146
	}
147
148
}
149