Completed
Push — master ( 131ba7...dc2999 )
by Dennis
06:08
created

src/Justice.php (1 issue)

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
    /**
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);
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
73
            }
74
        });
75
76
        return new JusticeRecord($people);
77
    }
78
79
    /**
80
     * @param Crawler $crawler
81
     *
82
     * @return false|string
83
     */
84
    private function extractDetailUrlFromCrawler(Crawler $crawler)
85
    {
86
        $linksFound = $crawler->filter('.result-links > li > a');
87
        if (!$linksFound) {
88
            return false;
89
        }
90
91
        $href = $linksFound->extract(['href']);
92
        if (!isset($href[1])) {
93
            return false;
94
        }
95
96
        return self::URL_BASE.$href[1];
97
    }
98
}
99