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

RelatedEntity   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 56.25%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 44
ccs 9
cts 16
cp 0.5625
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 4
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