1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SomeWork\Minjust; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use SomeWork\Minjust\Entity\Lawyer; |
9
|
|
|
use SomeWork\Minjust\Parser\ParserInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @see \SomeWork\Minjust\Tests\Unit\ServiceTest |
13
|
|
|
*/ |
14
|
|
|
class Service |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ParserInterface |
23
|
|
|
*/ |
24
|
|
|
private $parser; |
25
|
|
|
|
26
|
1 |
|
public function __construct(Client $client, ParserInterface $parser) |
27
|
|
|
{ |
28
|
1 |
|
$this->client = $client; |
29
|
1 |
|
$this->parser = $parser; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param FindRequest $findRequest |
34
|
|
|
* |
35
|
|
|
* @return Generator |
36
|
|
|
*/ |
37
|
1 |
|
public function findAll(FindRequest $findRequest): Generator |
38
|
|
|
{ |
39
|
1 |
|
return $this->findFromTo($findRequest, 1, 0); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param FindRequest $findRequest |
44
|
|
|
* @param int $startPage |
45
|
|
|
* @param int $endPage |
46
|
|
|
* |
47
|
|
|
* @return Generator |
48
|
|
|
* @todo Упростить логику метода |
49
|
|
|
*/ |
50
|
3 |
|
public function findFromTo(FindRequest $findRequest, int $startPage = 1, int $endPage = 1): Generator |
51
|
|
|
{ |
52
|
3 |
|
$findRequest->setPage($startPage); |
53
|
3 |
|
$findResponse = $this->find($findRequest); |
54
|
|
|
|
55
|
3 |
|
yield from $findRequest->isFullData() ? $findResponse->getDetailLawyers() : $findResponse->getLawyers(); |
56
|
3 |
|
if ($findResponse->getTotalPage() === 1) { |
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$endPage = $endPage ?: $findResponse->getTotalPage(); |
61
|
|
|
|
62
|
2 |
|
for ($i = $findResponse->getPage(); $i < $endPage; $i++) { |
63
|
2 |
|
$endPage = $endPage < $findResponse->getTotalPage() ? $endPage : $findResponse->getTotalPage(); |
64
|
2 |
|
$findRequest->setPage($i); |
65
|
2 |
|
yield from $findRequest->isFullData() ? |
66
|
1 |
|
$this->find($findRequest)->getDetailLawyers() : |
67
|
1 |
|
$this->find($findRequest)->getLawyers(); |
68
|
|
|
} |
69
|
2 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param FindRequest $findRequest |
73
|
|
|
* |
74
|
|
|
* @return FindResponse |
75
|
|
|
*/ |
76
|
1 |
|
public function find(FindRequest $findRequest): FindResponse |
77
|
|
|
{ |
78
|
1 |
|
$findResponse = $this->parser->list( |
79
|
1 |
|
$this->client->list($findRequest->getFormData()) |
80
|
|
|
); |
81
|
|
|
|
82
|
1 |
|
$findResponse->setDetailLawyersGenerator( |
83
|
1 |
|
$this->getDetailLawyersGenerator($findResponse->getLawyers()) |
84
|
|
|
); |
85
|
|
|
|
86
|
1 |
|
return $findResponse; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Lawyer[] $lawyers |
91
|
|
|
* |
92
|
|
|
* @return Generator |
93
|
|
|
*/ |
94
|
2 |
|
protected function getDetailLawyersGenerator(array $lawyers): Generator |
95
|
|
|
{ |
96
|
2 |
|
foreach ($lawyers as $lawyer) { |
97
|
|
|
yield $this |
98
|
1 |
|
->parser |
99
|
1 |
|
->detail($this->client->detail($lawyer->getUrl())) |
100
|
1 |
|
->loadFromLawyer($lawyer); |
101
|
|
|
} |
102
|
1 |
|
} |
103
|
|
|
} |
104
|
|
|
|