Completed
Push — master ( 91d607...0132ab )
by Milan
01:47
created

Ares::processXml()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 2
1
<?php
2
3
namespace h4kuna\Ares;
4
5
use GuzzleHttp;
6
7
/**
8
 * @author Milan Matějček <[email protected]>
9
 */
10
class Ares
11
{
12
13
	const URL = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=';
14
15
	/** @var DataProvider */
16
	private $dataProvider;
17
18
	public function __construct(DataProvider $dataProvider = NULL)
19
	{
20
		if ($dataProvider === NULL) {
21
			$dataProvider = $this->createDataProvider();
22
		}
23
		$this->dataProvider = $dataProvider;
24
	}
25
26
	/**
27
	 * Load fresh data.
28
	 * @param int|string $inn
29
	 * @throws InNotFoundExceptions
30
	 * @return IData
31
	 */
32
	public function loadData($inn)
33
	{
34
		$this->loadXML($inn);
35
		return $this->getData();
36
	}
37
38
	/**
39
	 * Get temporary data.
40
	 * @return IData
41
	 */
42
	public function getData()
43
	{
44
		return $this->dataProvider->getData();
45
	}
46
47
	/**
48
	 * Load XML and fill Data object
49
	 * @param string $inn
50
	 * @throws InNotFoundExceptions
51
	 */
52
	private function loadXML($inn)
53
	{
54
		$client = new GuzzleHttp\Client();
55
		$xmlSource = $client->request('GET', self::URL . (string) $inn)->getBody();
56
		$xml = @simplexml_load_string($xmlSource);
57
		if (!$xml) {
58
			throw new InNotFoundExceptions;
59
		}
60
61
		$ns = $xml->getDocNamespaces();
62
		$xmlEl = $xml->children($ns['are'])->children($ns['D'])->VBAS;
63
64
		if (!isset($xmlEl->ICO)) {
65
			throw new InNotFoundExceptions;
66
		}
67
68
		$this->processXml($xmlEl, $this->dataProvider->prepareData());
69
	}
70
71
	protected function processXml($xml, DataProvider $dataProvider)
72
	{
73
		$dataProvider->setIN($xml->ICO)
74
			->setTIN($xml->DIC)
75
			->setCity($xml->AA->N)
76
			->setCompany($xml->OF)
77
			->setStreet($xml->AD->UC, $xml->AA->NCO, isset($xml->AA->CO) ? $xml->AA->CO : NULL)
78
			->setZip($xml->AA->PSC)
79
			->setPerson($xml->PF->KPF)
80
			->setCreated($xml->DV);
81
82
		if (isset($xml->ROR)) {
83
			$dataProvider->setActive($xml->ROR->SOR->SSU)
84
				->setFileNumber($xml->ROR->SZ->OV)
85
				->setCourt($xml->ROR->SZ->SD->T);
86
		}
87
	}
88
89
	private function createDataProvider()
90
	{
91
		return new DataProvider(new DataFactory());
92
	}
93
94
}
95