Completed
Pull Request — master (#10)
by
unknown
02:01
created

Ares   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 5
dl 0
loc 144
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A isActiveMode() 0 4 1
A createUrl() 0 9 2
A getData() 0 4 1
A loadData() 0 9 2
A loadXML() 0 22 4
A processXml() 0 28 3
A getDataProvider() 0 7 2
A exists() 0 4 2
A existsArray() 0 4 2
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
	const URL = 'https://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
	 * @throws IdentificationNumberNotFoundException
35
	 */
36
	public function loadData(string $in, array $options = []): Data
37
	{
38
		try {
39
			$this->loadXML($in, $options, true);
40
		} catch (IdentificationNumberNotFoundException $e) {
41
			$this->loadXML($in, $options, false);
42
		}
43
		return $this->getData();
44
	}
45
46
47
	/**
48
	 * Get temporary data.
49
	 */
50
	public function getData(): Data
51
	{
52
		return $this->getDataProvider()->getData();
53
	}
54
55
56
	/**
57
	 * Load XML and fill Data object
58
	 * @throws IdentificationNumberNotFoundException
59
	 */
60
	private function loadXML(string $in, array $options, bool $activeOnly)
61
	{
62
		$client = $this->factory->createGuzzleClient($options);
63
		try {
64
			$xmlSource = $client->request('GET', $this->createUrl($in, $activeOnly))->getBody()->getContents();
65
		} catch (\Exception $e) {
66
			throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
67
		}
68
		$xml = @simplexml_load_string($xmlSource);
69
		if (!$xml) {
70
			throw new IdentificationNumberNotFoundException($in);
71
		}
72
73
		$ns = $xml->getDocNamespaces();
74
		$xmlEl = $xml->children($ns['are'])->children($ns['D'])->VBAS;
75
76
		if (!isset($xmlEl->ICO)) {
77
			throw new IdentificationNumberNotFoundException($in);
78
		}
79
80
		$this->processXml($xmlEl, $this->getDataProvider()->prepareData());
81
	}
82
83
84
	protected function processXml(\SimpleXMLElement $xml, DataProvider $dataProvider): void
85
	{
86
		$dataProvider->setIN((string) $xml->ICO)
87
			->setTIN((string) $xml->DIC)
88
			->setCompany((string) $xml->OF)
89
			->setZip(self::exists($xml->AA, 'PSC'))
90
			->setStreet(self::exists($xml->AA, 'NU'))
91
			->setCity(self::exists($xml->AA, 'N'))
92
			->setHouseNumber(self::exists($xml->AA, 'CD'), self::exists($xml->AA, 'CO'))
93
			->setCityPost(self::exists($xml->AA, 'NMC'))
94
			->setCityDistrict(self::exists($xml->AA, 'NCO'))
95
			->setIsPerson(self::exists($xml->PF, 'KPF'))
96
			->setCreated((string) $xml->DV)
97
			->setNace(self::existsArray($xml->Nace, 'NACE'));
98
99
		if (isset($xml->ROR)) {
100
			$dataProvider->setActive((string) $xml->ROR->SOR->SSU)
101
				->setFileNumber((string) $xml->ROR->SZ->OV)
102
				->setCourt((string) $xml->ROR->SZ->SD->T);
103
		} else {
104
			$dataProvider->setActive($this->activeMode)
105
				->setFileNumber('')
106
				->setCourt('');
107
		}
108
		if (!$this->isActiveMode()) {
109
			$dataProvider->setActive(false);
110
		}
111
	}
112
113
114
	protected function isActiveMode(): bool
115
	{
116
		return $this->activeMode === true;
117
	}
118
119
120
	private function createUrl(string $inn, bool $activeOnly): string
121
	{
122
		$this->activeMode = $activeOnly;
123
		$parameters = [
124
			'ico' => $inn,
125
			'aktivni' => $activeOnly ? 'true' : 'false',
126
		];
127
		return self::URL . '?' . http_build_query($parameters);
128
	}
129
130
131
	private function getDataProvider(): DataProvider
132
	{
133
		if ($this->dataProvider === null) {
134
			$this->dataProvider = $this->factory->createDataProvider();
135
		}
136
		return $this->dataProvider;
137
	}
138
139
140
	private static function exists(\SimpleXMLElement $element, string $property): string
141
	{
142
		return isset($element->{$property}) ? ((string) $element->{$property}) : '';
143
	}
144
	
145
	
146
	private static function existsArray(\SimpleXMLElement $element, string $property): array
147
	{
148
		return isset($element->{$property}) ? ((array) $element->{$property}) : [];
149
	}
150
151
}
152