RelatedEntity::__construct()   B
last analyzed

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
     * @var null|\mrcnpdlk\MojePanstwo\Model\KrsEntity
41
     */
42
    public $podmiot;
43
44
    /**
45
     * RelatedEntity constructor.
46
     *
47
     * @param string $str
48
     *
49
     * @throws \mrcnpdlk\MojePanstwo\Exception
50
     */
51 1
    public function __construct(string $str)
52
    {
53 1
        $this->opis = strip_tags($str);
54
        try {
55 1
            $aElem = HtmlDomParser::str_get_html($str)->getElementByTagName('a');
56 1
            if ($aElem) {
57 1
                $sHref = $aElem->getAttribute('href');
58 1
                $id    = (int)str_replace('/dane/krs_podmioty/', '', $sHref);
59 1
                if ($id > 0) {
60 1
                    $this->podmiot_id = $id;
61
                } else {
62
                    throw new \RuntimeException('KRS id not found in href');
63
                }
64
            }
65
        } catch (\Exception $e) {
66
            $this->podmiot_id = null;
67
            Api::getInstance()
68
               ->getClient()
69
               ->getLogger()
70
               ->warning(sprintf('Related entity [%s] Error: %s', $str, $e->getMessage()))
71
            ;
72
        }
73 1
    }
74
}
75