These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Defr; |
||
4 | |||
5 | use Defr\Ares\AresException; |
||
6 | use Defr\Ares\AresRecord; |
||
7 | use Defr\Ares\AresRecords; |
||
8 | use Defr\Ares\TaxRecord; |
||
9 | use InvalidArgumentException; |
||
10 | |||
11 | /** |
||
12 | * Class Ares. |
||
13 | * |
||
14 | * @author Dennis Fridrich <[email protected]> |
||
15 | */ |
||
16 | class Ares |
||
17 | { |
||
18 | const URL_BAS = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_bas.cgi?ico=%s'; |
||
19 | const URL_RES = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_res.cgi?ICO=%s'; |
||
20 | const URL_TAX = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/ares_es.cgi?ico=%s&filtr=0'; |
||
21 | const URL_FIND = 'http://wwwinfo.mfcr.cz/cgi-bin/ares/ares_es.cgi?obch_jm=%s&obec=%s&filtr=0'; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $cacheStrategy = 'YW'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $cacheDir = null; |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $debug; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $contextOptions = [ |
||
42 | 'ssl' => [ |
||
43 | 'verify_peer' => false, |
||
44 | 'verify_peer_name' => false, |
||
45 | ], |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @param null $cacheDir |
||
50 | * @param bool $debug |
||
51 | */ |
||
52 | 2 | public function __construct($cacheDir = null, $debug = false) |
|
53 | { |
||
54 | 2 | if ($cacheDir === null) { |
|
55 | 2 | $cacheDir = sys_get_temp_dir(); |
|
56 | } |
||
57 | |||
58 | 2 | $this->cacheDir = $cacheDir.'/defr/ares'; |
|
59 | 2 | $this->debug = $debug; |
|
60 | |||
61 | // Create cache dirs if they doesn't exist |
||
62 | 2 | if (!is_dir($this->cacheDir)) { |
|
63 | 1 | mkdir($this->cacheDir, 0777, true); |
|
64 | } |
||
65 | 2 | } |
|
66 | |||
67 | /** |
||
68 | * @param $id |
||
69 | * |
||
70 | * @throws InvalidArgumentException |
||
71 | * @throws Ares\AresException |
||
72 | * |
||
73 | * @return AresRecord |
||
74 | */ |
||
75 | 2 | public function findByIdentificationNumber($id) |
|
76 | { |
||
77 | 2 | $id = Lib::toInteger($id); |
|
78 | 2 | $this->ensureIdIsInteger($id); |
|
79 | |||
80 | 2 | $cachedFileName = $id.'_'.date($this->cacheStrategy).'.php'; |
|
81 | 2 | $cachedFile = $this->cacheDir.'/bas_'.$cachedFileName; |
|
82 | 2 | $cachedRawFile = $this->cacheDir.'/bas_raw_'.$cachedFileName; |
|
83 | |||
84 | 2 | if (is_file($cachedFile)) { |
|
85 | return unserialize(file_get_contents($cachedFile)); |
||
86 | } |
||
87 | |||
88 | // Sestaveni URL |
||
89 | 2 | $url = sprintf(self::URL_BAS, $id); |
|
90 | |||
91 | try { |
||
92 | 2 | $aresRequest = file_get_contents($url, null, stream_context_create($this->contextOptions)); |
|
93 | 2 | if ($this->debug) { |
|
94 | file_put_contents($cachedRawFile, $aresRequest); |
||
95 | } |
||
96 | 2 | $aresResponse = simplexml_load_string($aresRequest); |
|
97 | |||
98 | 2 | if ($aresResponse) { |
|
99 | 2 | $ns = $aresResponse->getDocNamespaces(); |
|
100 | 2 | $data = $aresResponse->children($ns['are']); |
|
101 | 2 | $elements = $data->children($ns['D'])->VBAS; |
|
102 | |||
103 | 2 | $ico = (int) $elements->ICO; |
|
104 | 2 | if ($ico !== $id) { |
|
105 | throw new AresException('IČ firmy nebylo nalezeno.'); |
||
106 | } |
||
107 | |||
108 | 2 | $record = new AresRecord( |
|
109 | $id, |
||
110 | 2 | strval($elements->DIC), // taxId |
|
111 | 2 | strval($elements->OF), // companyName |
|
112 | 2 | strval($elements->AA->NU), // street |
|
113 | 1 | strval($elements->AA->CD), // house number |
|
114 | 1 | strval($elements->AA->CO) ?: null, // house orientation number |
|
115 | 1 | strval($elements->AA->NCO) ? strval($elements->AA->N.' - '.strval($elements->AA->NCO)) : strval($elements->AA->N), // town |
|
116 | 1 | strval($elements->AA->PSC) // ZIP |
|
117 | ); |
||
118 | |||
119 | } else { |
||
120 | 1 | throw new AresException('Databáze ARES není dostupná.'); |
|
121 | } |
||
122 | 1 | } catch (\Exception $e) { |
|
123 | 1 | throw new AresException($e->getMessage()); |
|
124 | } |
||
125 | |||
126 | 1 | file_put_contents($cachedFile, serialize($record)); |
|
127 | |||
128 | 1 | return $record; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param $id |
||
133 | * |
||
134 | * @throws InvalidArgumentException |
||
135 | * @throws Ares\AresException |
||
136 | * |
||
137 | * @return AresRecord |
||
138 | */ |
||
139 | public function findInResById($id) |
||
140 | { |
||
141 | $id = Lib::toInteger($id); |
||
142 | $this->ensureIdIsInteger($id); |
||
143 | |||
144 | // Sestaveni URL |
||
145 | $url = sprintf(self::URL_RES, $id); |
||
146 | |||
147 | $cachedFileName = $id.'_'.date($this->cacheStrategy).'.php'; |
||
148 | $cachedFile = $this->cacheDir.'/res_'.$cachedFileName; |
||
149 | $cachedRawFile = $this->cacheDir.'/res_raw_'.$cachedFileName; |
||
150 | |||
151 | if (is_file($cachedFile)) { |
||
152 | return unserialize(file_get_contents($cachedFile)); |
||
153 | } |
||
154 | |||
155 | try { |
||
156 | $aresRequest = file_get_contents($url, null, stream_context_create($this->contextOptions)); |
||
157 | if ($this->debug) { |
||
158 | file_put_contents($cachedRawFile, $aresRequest); |
||
159 | } |
||
160 | $aresResponse = simplexml_load_string($aresRequest); |
||
161 | |||
162 | if ($aresResponse) { |
||
163 | $ns = $aresResponse->getDocNamespaces(); |
||
164 | $data = $aresResponse->children($ns['are']); |
||
165 | $elements = $data->children($ns['D'])->Vypis_RES; |
||
166 | |||
167 | if (strval($elements->ZAU->ICO) === $id) { |
||
168 | $record = new AresRecord( |
||
169 | strval($id), |
||
170 | $this->findVatById($id), |
||
171 | strval($elements->ZAU->OF), |
||
172 | strval($elements->SI->NU), |
||
173 | strval($elements->SI->CD), |
||
174 | strval($elements->SI->CO), |
||
175 | strval($elements->SI->N), |
||
176 | strval($elements->SI->PSC) |
||
177 | ); |
||
178 | } else { |
||
179 | throw new AresException('IČ firmy nebylo nalezeno.'); |
||
180 | } |
||
181 | } else { |
||
182 | throw new AresException('Databáze ARES není dostupná.'); |
||
183 | } |
||
184 | } catch (\Exception $e) { |
||
185 | throw new AresException($e->getMessage()); |
||
186 | } |
||
187 | file_put_contents($cachedFile, serialize($record)); |
||
188 | |||
189 | return $record; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param $id |
||
194 | * |
||
195 | * @throws InvalidArgumentException |
||
196 | * @throws \Exception |
||
197 | * |
||
198 | * @return TaxRecord|mixed |
||
199 | */ |
||
200 | public function findVatById($id) |
||
201 | { |
||
202 | $id = Lib::toInteger($id); |
||
203 | |||
204 | $this->ensureIdIsInteger($id); |
||
205 | |||
206 | // Sestaveni URL |
||
207 | $url = sprintf(self::URL_TAX, $id); |
||
208 | |||
209 | $cachedFileName = $id.'_'.date($this->cacheStrategy).'.php'; |
||
210 | $cachedFile = $this->cacheDir.'/tax_'.$cachedFileName; |
||
211 | $cachedRawFile = $this->cacheDir.'/tax_raw_'.$cachedFileName; |
||
212 | |||
213 | if (is_file($cachedFile)) { |
||
214 | return unserialize(file_get_contents($cachedFile)); |
||
215 | } |
||
216 | |||
217 | try { |
||
218 | $vatRequest = file_get_contents($url, null, stream_context_create($this->contextOptions)); |
||
219 | if ($this->debug) { |
||
220 | file_put_contents($cachedRawFile, $vatRequest); |
||
221 | } |
||
222 | $vatResponse = simplexml_load_string($vatRequest); |
||
223 | |||
224 | if ($vatResponse) { |
||
225 | |||
226 | $ns = $vatResponse->getDocNamespaces(); |
||
227 | $data = $vatResponse->children($ns['are']); |
||
228 | $elements = $data->children($ns['dtt'])->V->S; |
||
229 | |||
230 | if (strval($elements->ico) === $id) { |
||
231 | $record = new TaxRecord( |
||
232 | str_replace('dic=', 'CZ', strval($elements->p_dph)) |
||
233 | ); |
||
234 | } else { |
||
235 | throw new AresException('DIČ firmy nebylo nalezeno.'); |
||
236 | } |
||
237 | } else { |
||
238 | throw new AresException('Databáze MFČR není dostupná.'); |
||
239 | } |
||
240 | } catch (\Exception $e) { |
||
241 | throw new \Exception($e->getMessage()); |
||
242 | } |
||
243 | file_put_contents($cachedFile, serialize($record)); |
||
244 | |||
245 | return $record; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param $name |
||
250 | * @param null $city |
||
251 | * |
||
252 | * @throws InvalidArgumentException |
||
253 | * @throws \Exception |
||
254 | * |
||
255 | * @return array|AresRecord[]|AresRecords |
||
256 | */ |
||
257 | public function findByName($name, $city = null) |
||
258 | { |
||
259 | if (strlen($name) < 3) { |
||
260 | throw new InvalidArgumentException('Zadejte minimálně 3 znaky pro hledání.'); |
||
261 | } |
||
262 | |||
263 | $url = sprintf( |
||
264 | self::URL_FIND, |
||
265 | urlencode(Lib::stripDiacritics($name)), |
||
266 | urlencode(Lib::stripDiacritics($city)) |
||
267 | ); |
||
268 | |||
269 | $cachedFileName = date($this->cacheStrategy).'_'.md5($name.$city).'.php'; |
||
270 | $cachedFile = $this->cacheDir.'/find_'.$cachedFileName; |
||
271 | $cachedRawFile = $this->cacheDir.'/find_raw_'.$cachedFileName; |
||
272 | |||
273 | if (is_file($cachedFile)) { |
||
274 | return unserialize(file_get_contents($cachedFile)); |
||
275 | } |
||
276 | |||
277 | $aresRequest = file_get_contents($url, null, stream_context_create($this->contextOptions)); |
||
278 | if ($this->debug) { |
||
279 | file_put_contents($cachedRawFile, $aresRequest); |
||
280 | } |
||
281 | $aresResponse = simplexml_load_string($aresRequest); |
||
282 | if (!$aresResponse) { |
||
283 | throw new AresException('Databáze ARES není dostupná.'); |
||
284 | } |
||
285 | |||
286 | $ns = $aresResponse->getDocNamespaces(); |
||
287 | $data = $aresResponse->children($ns['are']); |
||
288 | $elements = $data->children($ns['dtt'])->V->S; |
||
289 | |||
290 | if (!count($elements)) { |
||
291 | throw new AresException('Nic nebylo nalezeno.'); |
||
292 | } |
||
293 | |||
294 | $records = new AresRecords(); |
||
295 | foreach ($elements as $element) { |
||
296 | // TODO: What is this? |
||
297 | $record = new AresRecord(); |
||
298 | $record->setCompanyId(strval($element->ico)); |
||
0 ignored issues
–
show
|
|||
299 | $record->setTaxId( |
||
0 ignored issues
–
show
The method
setTaxId() does not seem to exist on object<Defr\Ares\AresRecord> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
300 | ($element->dph ? str_replace('dic=', 'CZ', strval($element->p_dph)) : '') |
||
301 | ); |
||
302 | $record->setCompanyName(strval($element->ojm)); |
||
0 ignored issues
–
show
The method
setCompanyName() does not seem to exist on object<Defr\Ares\AresRecord> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||
303 | //'adresa' => strval($element->jmn)); |
||
304 | $records[] = $record; |
||
305 | } |
||
306 | file_put_contents($cachedFile, serialize($records)); |
||
307 | |||
308 | return $records; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param string $cacheStrategy |
||
313 | */ |
||
314 | public function setCacheStrategy($cacheStrategy) |
||
315 | { |
||
316 | $this->cacheStrategy = $cacheStrategy; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param bool $debug |
||
321 | */ |
||
322 | public function setDebug($debug) |
||
323 | { |
||
324 | $this->debug = $debug; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @param mixed $id |
||
329 | */ |
||
330 | 2 | private function ensureIdIsInteger($id) |
|
331 | { |
||
332 | 2 | if (!is_int($id)) { |
|
333 | throw new InvalidArgumentException('IČ firmy musí být číslo.'); |
||
334 | } |
||
335 | 2 | } |
|
336 | } |
||
337 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.