Justice::findById()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 26
cp 0
rs 8.439
cc 5
eloc 20
nc 2
nop 1
crap 30
1
<?php
2
3
namespace Defr;
4
5
use Assert\Assertion;
6
use Defr\Justice\JusticeRecord;
7
use Defr\Justice\SubjectNotFoundException;
8
use Defr\Parser\JusticeJednatelPersonParser;
9
use Defr\Parser\JusticeSpolecnikPersonParser;
10
use Goutte\Client;
11
use Symfony\Component\DomCrawler\Crawler;
12
13
final class Justice
14
{
15
    /**
16
     * @var string
17
     */
18
    const URL_BASE = 'https://or.justice.cz/ias/ui/';
19
20
    /**
21
     * @var string
22
     */
23
    const URL_SUBJECTS = 'https://or.justice.cz/ias/ui/rejstrik-$firma?ico=%d';
24
25
    /**
26
     * @var Client
27
     */
28
    private $client;
29
30
    /**
31
     * Justice constructor.
32
     *
33
     * @param Client $client
34
     */
35
    public function __construct(Client $client)
36
    {
37
        $this->client = $client;
38
    }
39
40
    /**
41
     * @param int $id
42
     *
43
     * @throws SubjectNotFoundException
44
     *
45
     * @return JusticeRecord|false
46
     */
47
    public function findById($id)
48
    {
49
        Assertion::integer($id);
50
51
        $crawler = $this->client->request('GET', sprintf(self::URL_SUBJECTS, $id));
52
        $detailUrl = $this->extractDetailUrlFromCrawler($crawler);
0 ignored issues
show
Bug introduced by
It seems like $crawler defined by $this->client->request('...lf::URL_SUBJECTS, $id)) on line 51 can be null; however, Defr\Justice::extractDetailUrlFromCrawler() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
53
54
        if (false === $detailUrl) {
55
            return false;
56
        }
57
58
        $people = [];
59
60
        $crawler = $this->client->request('GET', $detailUrl);
61
        $crawler->filter('.aunp-content .div-table')->each(function (Crawler $table) use (&$people) {
62
            $title = $table->filter('.vr-hlavicka')->text();
63
64
            try {
65
                if ('jednatel: ' === $title) {
66
                    $person = JusticeJednatelPersonParser::parseFromDomCrawler($table);
67
                    $people[$person->getName()] = $person;
68
                } elseif ('Společník: ' === $title) {
69
                    $person = JusticeSpolecnikPersonParser::parseFromDomCrawler($table);
70
                    $people[$person->getName()] = $person;
71
                }
72
            } catch (\Exception $e) {
73
                throw $e;
74
            }
75
        });
76
77
        return new JusticeRecord($people);
78
    }
79
80
    /**
81
     * @param Crawler $crawler
82
     *
83
     * @return false|string
84
     */
85
    private function extractDetailUrlFromCrawler(Crawler $crawler)
86
    {
87
        $linksFound = $crawler->filter('.result-links > li > a');
88
        if (!$linksFound) {
89
            return false;
90
        }
91
92
        $href = $linksFound->extract(['href']);
93
        if (!isset($href[1])) {
94
            return false;
95
        }
96
97
        return self::URL_BASE.$href[1];
98
    }
99
}
100