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