Completed
Push — master ( 201db3...6560f2 )
by Milan
01:44
created

Ares::isActiveMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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