Completed
Push — master ( d8106d...a88754 )
by Marcin
03:16
created

RelatedEntity::__construct()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.3398

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 9
cts 16
cp 0.5625
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 17
nc 6
nop 1
crap 5.3398
1
<?php
2
/**
3
 * MOJEPANSTWO-API
4
 *
5
 * Copyright © 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 */
14
15
/**
16
 * Created by Marcin.
17
 * Date: 02.12.2017
18
 * Time: 23:19
19
 */
20
21
namespace mrcnpdlk\MojePanstwo\Model\KrsPerson;
22
23
24
use mrcnpdlk\MojePanstwo\Api;
25
use Sunra\PhpSimple\HtmlDomParser;
26
27
class RelatedEntity
28
{
29
    /**
30
     * @var string
31
     */
32
    public $opis;
33
    /**
34
     * ID podmiotu KRS
35
     *
36
     * @var int
37
     */
38
    public $podmiot_id;
39
40
    /**
41
     * RelatedEntity constructor.
42
     *
43
     * @param string $str
44
     *
45
     * @throws \mrcnpdlk\MojePanstwo\Exception
46
     */
47 1
    public function __construct(string $str)
48
    {
49 1
        $this->opis = strip_tags($str);
50
        try {
51 1
            $aElem = HtmlDomParser::str_get_html($str)->getElementByTagName('a');
52 1
            if ($aElem) {
53 1
                $sHref = $aElem->getAttribute('href');
54 1
                $id    = (int)str_replace('/dane/krs_podmioty/', '', $sHref);
55 1
                if ($id > 0) {
56 1
                    $this->podmiot_id = $id;
57
                } else {
58
                    throw new \RuntimeException('KRS id not found in href');
59
                }
60
            }
61
        } catch (\Exception $e) {
62
            $this->podmiot_id = null;
63
            Api::getInstance()
64
               ->getClient()
65
               ->getLogger()
66
               ->warning(sprintf('Related entity [%s] Error: %s', $str, $e->getMessage()))
67
            ;
68
        }
69 1
    }
70
}
71