Completed
Push — master ( 4f911a...20d659 )
by Milan
06:11
created

Ares   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A loadData() 0 9 2
A getData() 0 4 1
A loadXML() 0 18 3
B processXml() 0 27 3
A isActiveMode() 0 4 1
A createUrl() 0 9 2
A createDataProvider() 0 4 1
A exists() 0 4 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';
14
15
	/** @var DataProvider */
16
	private $dataProvider;
17
18
	/** @var bool */
19
	private $activeMode;
20
21
	public function __construct(DataProvider $dataProvider = NULL)
22
	{
23
		if ($dataProvider === NULL) {
24
			$dataProvider = $this->createDataProvider();
25
		}
26
		$this->dataProvider = $dataProvider;
27
	}
28
29
	/**
30
	 * Load fresh data.
31
	 * @param int|string $in
32
	 * @return Data
33
	 * @throws IdentificationNumberNotFoundException
34
	 */
35
	public function loadData($in)
36
	{
37
		try {
38
			$this->loadXML((string) $in, TRUE);
39
		} catch (IdentificationNumberNotFoundException $e) {
40
			$this->loadXML((string) $in, FALSE);
41
		}
42
		return $this->getData();
43
	}
44
45
	/**
46
	 * Get temporary data.
47
	 * @return Data
48
	 */
49
	public function getData()
50
	{
51
		return $this->dataProvider->getData();
52
	}
53
54
	/**
55
	 * Load XML and fill Data object
56
	 * @param string $in
57
	 * @param bool $activeOnly
58
	 * @throws IdentificationNumberNotFoundException
59
	 */
60
	private function loadXML($in, $activeOnly)
61
	{
62
		$client = new GuzzleHttp\Client();
63
		$xmlSource = $client->request('GET', $this->createUrl($in, $activeOnly))->getBody();
64
		$xml = @simplexml_load_string($xmlSource);
65
		if (!$xml) {
66
			throw new IdentificationNumberNotFoundException($in);
67
		}
68
69
		$ns = $xml->getDocNamespaces();
70
		$xmlEl = $xml->children($ns['are'])->children($ns['D'])->VBAS;
71
72
		if (!isset($xmlEl->ICO)) {
73
			throw new IdentificationNumberNotFoundException($in);
74
		}
75
76
		$this->processXml($xmlEl, $this->dataProvider->prepareData());
77
	}
78
79
	protected function processXml($xml, DataProvider $dataProvider)
80
	{
81
		$dataProvider->setIN($xml->ICO)
82
			->setTIN($xml->DIC)
83
			->setCompany($xml->OF)
84
			->setZip(self::exists($xml->AA, 'PSC'))
85
			->setStreet(self::exists($xml->AA, 'NU'))
86
			->setCity(self::exists($xml->AA, 'N'))
87
			->setHouseNumber(self::exists($xml->AA, 'CD'), self::exists($xml->AA, 'CO'))
88
			->setCityPost(self::exists($xml->AA, 'NMC'))
89
			->setCityDistrict(self::exists($xml->AA, 'NCO'))
90
			->setIsPerson(self::exists($xml->PF, 'KPF'))
91
			->setCreated($xml->DV);
92
93
		if (isset($xml->ROR)) {
94
			$dataProvider->setActive($xml->ROR->SOR->SSU)
95
				->setFileNumber($xml->ROR->SZ->OV)
96
				->setCourt($xml->ROR->SZ->SD->T);
97
		} else {
98
			$dataProvider->setActive($this->activeMode)
0 ignored issues
show
Documentation introduced by
$this->activeMode is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
				->setFileNumber('')
100
				->setCourt('');
101
		}
102
		if (!$this->isActiveMode()) {
103
			$dataProvider->setActive('no');
104
		}
105
	}
106
107
	protected function isActiveMode()
108
	{
109
		return $this->activeMode === TRUE;
110
	}
111
112
	private function createUrl($inn, $activeOnly)
113
	{
114
		$this->activeMode = (bool) $activeOnly;
115
		$parameters = [
116
			'ico' => $inn,
117
			'aktivni' => $activeOnly ? 'true' : 'false'
118
		];
119
		return self::URL . '?' . http_build_query($parameters);
120
	}
121
122
	private function createDataProvider()
123
	{
124
		return new DataProvider(new DataFactory());
125
	}
126
127
	private static function exists($element, $property)
128
	{
129
		return isset($element->{$property}) ? $element->{$property} : '';
130
	}
131
132
}
133