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