Completed
Push — master ( d085b4...8770d9 )
by Dennis
06:04 queued 03:00
created

src/Justice.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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 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
    public function __construct(Client $client)
31
    {
32
        $this->client = $client;
33
    }
34
35
    /**
36
     * @param int $id
37
     *
38
     * @throws SubjectNotFoundException
39
     *
40
     * @return JusticeRecord|false
41
     */
42
    public function findById($id)
43
    {
44
        Assertion::integer($id);
45
46
        $crawler = $this->client->request('GET', sprintf(self::URL_SUBJECTS, $id));
47
        $detailUrl = $this->extractDetailUrlFromCrawler($crawler);
0 ignored issues
show
It seems like $crawler defined by $this->client->request('...lf::URL_SUBJECTS, $id)) on line 46 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...
48
49
        if (false === $detailUrl) {
50
            return false;
51
        }
52
53
        $people = [];
54
55
        $crawler = $this->client->request('GET', $detailUrl);
56
        $crawler->filter('.aunp-content .div-table')->each(function (Crawler $table) use (&$people) {
57
            $title = $table->filter('.vr-hlavicka')->text();
58
59
            if ('jednatel: ' === $title) {
60
                $person = JusticeJednatelPersonParser::parseFromDomCrawler($table);
61
                $people[$person->getName()] = $person;
62
            } elseif ('Společník: ' === $title) {
63
                $person = JusticeSpolecnikPersonParser::parseFromDomCrawler($table);
64
                $people[$person->getName()] = $person;
65
            }
66
        });
67
68
        return new JusticeRecord($people);
69
    }
70
71
    /**
72
     * @return int|false
73
     */
74
    private function extractDetailUrlFromCrawler(Crawler $crawler)
75
    {
76
        $linksFound = $crawler->filter('.result-links > li > a');
77
        if (!$linksFound) {
78
            return false;
79
        }
80
81
        $href = $linksFound->extract(['href']);
82
        if (!isset($href[1])) {
83
            return false;
84
        }
85
86
        return self::URL_BASE.$href[1];
87
    }
88
}
89