Passed
Push — master ( 95e5b7...727475 )
by Igor
03:25
created

DomParser::getLawFormation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace SomeWork\Minjust\Parser;
4
5
use PHPHtmlParser\Dom;
6
use PHPHtmlParser\Dom\Collection;
7
use PHPHtmlParser\Dom\HtmlNode;
8
use PHPHtmlParser\Exceptions\ChildNotFoundException;
9
use PHPHtmlParser\Exceptions\NotLoadedException;
10
use SomeWork\Minjust\Entity\DetailLawyer;
11
use SomeWork\Minjust\Entity\LawFormation;
12
use SomeWork\Minjust\Entity\Lawyer;
13
use SomeWork\Minjust\FindResponse;
14
15
class DomParser implements ParserInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected const CURRENT_PAGE_SELECTOR = 'span.currentStep';
21
22
    /**
23
     * @var string
24
     */
25
    protected const PAGINATION_BLOCK_SELECTOR = 'div.pagination';
26
27
    /**
28
     * @var string
29
     */
30
    protected const PAGINATION_STEP_SELECTOR = 'a.step';
31
32
    /**
33
     * @var string
34
     */
35
    protected const LAWYERS_LIST_BLOCK_SELECTOR = 'table.persons > tbody > tr';
36
37
    /**
38
     * @var string
39
     */
40
    protected const LAWYER_DETAIL_SELECTOR = '.floating > p.row';
41
42 5
    public function list(string $body): FindResponse
43
    {
44 5
        $dom = (new Dom())->loadStr($body);
45
46 5
        return (new FindResponse())
47 5
            ->setPage($this->getCurrentPage($dom))
48 5
            ->setTotalPage($this->getTotalPage($dom))
49 5
            ->setLawyers($this->getListLawyers($dom));
50
    }
51
52 5
    protected function getCurrentPage(Dom $dom): int
53
    {
54 5
        if ($span = $dom->find(static::CURRENT_PAGE_SELECTOR, 0)) {
55 4
            return (int) $span->text();
56
        }
57
58 1
        return 1;
59
    }
60
61 4
    protected function getTotalPage(Dom $dom): int
62
    {
63
        /**
64
         * @var HtmlNode[] $collection
65
         */
66
        $collection = $dom
67 4
            ->find(static::PAGINATION_BLOCK_SELECTOR, 0)
68 4
            ->find(static::PAGINATION_STEP_SELECTOR)
69 4
            ->toArray();
70 4
        if (0 === count($collection)) {
71 1
            return 1;
72
        }
73 3
        $lastStep = (int) end($collection)->text();
74 3
        $currentPage = $this->getCurrentPage($dom);
75
76 3
        return $lastStep > $currentPage ? $lastStep : $currentPage;
77
    }
78
79
    /**
80
     * @param \PHPHtmlParser\Dom $dom
81
     *
82
     * @return Lawyer[]
83
     * @throws ChildNotFoundException
84
     * @throws NotLoadedException
85
     */
86 5
    protected function getListLawyers(Dom $dom): array
87
    {
88 5
        $data = [];
89
        /**
90
         * @var Dom\HtmlNode[]|Collection $nodes
91
         */
92 5
        $nodes = $dom->find(static::LAWYERS_LIST_BLOCK_SELECTOR);
93 5
        foreach ($nodes as $node) {
94
            /**
95
             * @var Dom\HtmlNode[]|Collection $tds
96
             */
97 5
            $tds = $node->find('td');
98
            $tds = array_filter($tds->toArray(), static function (HtmlNode $node) {
99 5
                return $node->outerHtml() !== '' && $node->getAttribute('class') !== 'empty';
100 5
            });
101 5
            $data[] = (new Lawyer())
102 5
                ->setRegisterNumber($tds[3]->text())
103 5
                ->setFullName($tds[4]->text(true))
104 5
                ->setUrl($tds[4]->firstChild()->getAttribute('href'))
105 5
                ->setTerritorialSubject($tds[5]->text())
106 5
                ->setCertificateNumber($tds[6]->text())
107 5
                ->setStatus($tds[7]->text());
108
        }
109
110 5
        return $data;
111
    }
112
113 2
    public function detail(string $body): DetailLawyer
114
    {
115 2
        $dom = (new Dom())->loadStr($body);
116
117
        /**
118
         * @var Dom\HtmlNode[] $nodes
119
         */
120 2
        $nodes = $dom->find(static::LAWYER_DETAIL_SELECTOR)->toArray();
121
122 2
        $lawyer = (new DetailLawyer())
123 2
            ->setChamberOfLaw(trim($nodes[5]->text()));
124 2
        if (($organizationForm = trim($nodes[7]->text())) !== '') {
125 1
            $lawyer->setLawFormation(
126 1
                (new LawFormation())
127 1
                    ->setOrganizationalForm($organizationForm)
128 1
                    ->setName(trim($nodes[9]->text()))
129 1
                    ->setAddress(trim($nodes[11]->text()))
130 1
                    ->setPhone(trim($nodes[13]->text()))
131 1
                    ->setEmail(trim($nodes[15]->text()))
132
            );
133
        }
134
135 2
        return $lawyer;
136
    }
137
}
138