Completed
Push — master ( 110ae7...195c3e )
by Milan
02:04
created

Ares::isActiveMode()   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
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
	 * @param bool $activeOnly
0 ignored issues
show
Bug introduced by
There is no parameter named $activeOnly. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
33
	 * @return IData
34
	 * @throws IdentificationNumberNotFoundException
35
	 */
36
	public function loadData($in)
37
	{
38
		try {
39
			$this->loadXML((string) $in, TRUE);
40
		} catch (IdentificationNumberNotFoundException $e) {
41
			$this->loadXML((string) $in, FALSE);
42
		}
43
		return $this->getData();
44
	}
45
46
	/**
47
	 * Get temporary data.
48
	 * @return IData
49
	 */
50
	public function getData()
51
	{
52
		return $this->dataProvider->getData();
53
	}
54
55
	/**
56
	 * Load XML and fill Data object
57
	 * @param string $in
58
	 * @param bool $activeOnly
59
	 * @throws IdentificationNumberNotFoundException
60
	 */
61
	private function loadXML($in, $activeOnly)
62
	{
63
		$client = new GuzzleHttp\Client();
64
		$xmlSource = $client->request('GET', $this->createUrl($in, $activeOnly))->getBody();
65
		$xml = @simplexml_load_string($xmlSource);
66
		if (!$xml) {
67
			throw new IdentificationNumberNotFoundException($in);
68
		}
69
70
		$ns = $xml->getDocNamespaces();
71
		$xmlEl = $xml->children($ns['are'])->children($ns['D'])->VBAS;
72
73
		if (!isset($xmlEl->ICO)) {
74
			throw new IdentificationNumberNotFoundException($in);
75
		}
76
77
		$this->processXml($xmlEl, $this->dataProvider->prepareData());
78
	}
79
80
	protected function processXml($xml, DataProvider $dataProvider)
81
	{
82
		$dataProvider->setIN($xml->ICO)
83
			->setTIN($xml->DIC)
84
			->setCity($xml->AA->N)
85
			->setCompany($xml->OF)
86
			->setStreet($xml->AD->UC, $xml->AA->NCO, isset($xml->AA->CO) ? $xml->AA->CO : NULL)
87
			->setZip($xml->AA->PSC)
88
			->setPerson($xml->PF->KPF)
89
			->setCreated($xml->DV);
90
91
		if (isset($xml->ROR)) {
92
			$dataProvider->setActive($xml->ROR->SOR->SSU)
93
				->setFileNumber($xml->ROR->SZ->OV)
94
				->setCourt($xml->ROR->SZ->SD->T);
95
		}
96
		if (!$this->isActiveMode()) {
97
			$dataProvider->setActive(FALSE);
0 ignored issues
show
Documentation introduced by
FALSE 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...
98
		}
99
	}
100
101
	protected function isActiveMode()
102
	{
103
		return $this->activeMode === TRUE;
104
	}
105
106
	private function createUrl($inn, $activeOnly)
107
	{
108
		$this->activeMode = (bool) $activeOnly;
109
		$parameters = [
110
			'ico' => $inn,
111
			'aktivni' => $activeOnly ? 'true' : 'false'
112
		];
113
		return self::URL . '?' . http_build_query($parameters);
114
	}
115
116
	private function createDataProvider()
117
	{
118
		return new DataProvider(new DataFactory());
119
	}
120
121
}
122