1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Veslo project <https://github.com/symfony-doge/veslo>. |
5
|
|
|
* |
6
|
|
|
* (C) 2019 Pavel Petrov <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Veslo\AnthillBundle\Vacancy\Parser; |
17
|
|
|
|
18
|
|
|
use Closure; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
21
|
|
|
use Veslo\AnthillBundle\Dto\Vacancy\LocationDto; |
22
|
|
|
use Veslo\AnthillBundle\Vacancy\ScannerPool\ConveyorAwareScannerPool; |
23
|
|
|
use Veslo\AppBundle\Workflow\Vacancy\PitInterface; |
24
|
|
|
use Veslo\AppBundle\Workflow\Vacancy\Worker\Iteration; |
25
|
|
|
use Veslo\AppBundle\Workflow\Vacancy\WorkerInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* TODO: ascii image and descr |
29
|
|
|
*/ |
30
|
|
|
class Earwig implements WorkerInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Logger as it is |
34
|
|
|
* |
35
|
|
|
* @var LoggerInterface |
36
|
|
|
*/ |
37
|
|
|
private $logger; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Converts an object into a set of arrays/scalars |
41
|
|
|
* |
42
|
|
|
* @var NormalizerInterface |
43
|
|
|
*/ |
44
|
|
|
private $normalizer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Aggregates scanners for vacancy parsing in context of workflow conveyor |
48
|
|
|
* |
49
|
|
|
* @var ConveyorAwareScannerPool |
50
|
|
|
*/ |
51
|
|
|
private $scannerPool; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Storage for parsed vacancy from website-provider |
55
|
|
|
* Destination place in which result of worker action will be persisted |
56
|
|
|
* |
57
|
|
|
* @var PitInterface |
58
|
|
|
*/ |
59
|
|
|
private $destination; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Earwig constructor. |
63
|
|
|
* |
64
|
|
|
* @param LoggerInterface $logger Logger as it is |
65
|
|
|
* @param NormalizerInterface $normalizer Converts an object into a set of arrays/scalars |
66
|
|
|
* @param ConveyorAwareScannerPool $scannerPool Aggregates scanners for vacancy parsing in conveyor context |
67
|
|
|
* @param PitInterface $destination Storage for parsed vacancy from website-provider |
68
|
|
|
*/ |
69
|
|
|
public function __construct( |
70
|
|
|
LoggerInterface $logger, |
71
|
|
|
NormalizerInterface $normalizer, |
72
|
|
|
ConveyorAwareScannerPool $scannerPool, |
73
|
|
|
PitInterface $destination |
74
|
|
|
) { |
75
|
|
|
$this->logger = $logger; |
76
|
|
|
$this->normalizer = $normalizer; |
77
|
|
|
$this->scannerPool = $scannerPool; |
78
|
|
|
$this->destination = $destination; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Performs dung (vacancies) parsing by specified source and attempts count |
83
|
|
|
* |
84
|
|
|
* @param PitInterface $pit Vacancy URL storage |
85
|
|
|
* @param int $iterations Parsing iterations count, at least one expected |
86
|
|
|
* |
87
|
|
|
* @return int Successful parse iterations count |
88
|
|
|
*/ |
89
|
|
|
public function parse(PitInterface $pit, int $iterations = 1): int |
90
|
|
|
{ |
91
|
|
|
$sourceName = get_class($pit); |
92
|
|
|
|
93
|
|
|
$this->logger->debug('Parsing started.', ['source' => $sourceName, 'iterations' => $iterations]); |
94
|
|
|
|
95
|
|
|
$iteration = Closure::fromCallable([$this, 'parseIteration']); |
96
|
|
|
$iterationLoop = new Iteration\Loop($this, $iteration, 'An error has been occurred during vacancy parsing.'); |
97
|
|
|
|
98
|
|
|
$successfulIterations = $iterationLoop->execute($pit, $iterations); |
99
|
|
|
|
100
|
|
|
$this->logger->debug( |
101
|
|
|
'Parsing completed.', |
102
|
|
|
[ |
103
|
|
|
'source' => $sourceName, |
104
|
|
|
'iterations' => $iterations, |
105
|
|
|
'successful' => $successfulIterations, |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
return $successfulIterations; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getLogger(): ?LoggerInterface |
116
|
|
|
{ |
117
|
|
|
return $this->logger; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns positive if vacancy is successfully parsed |
122
|
|
|
* Builds and sends a payload to conveyor for further processing according to configured workflow |
123
|
|
|
* |
124
|
|
|
* @param PitInterface $pit Vacancy URL storage |
125
|
|
|
* |
126
|
|
|
* @return bool Positive, if vacancy data has been successfully parsed, negative otherwise |
127
|
|
|
*/ |
128
|
|
|
private function parseIteration(PitInterface $pit): bool |
129
|
|
|
{ |
130
|
|
|
$sourceName = get_class($pit); |
131
|
|
|
|
132
|
|
|
/** @var LocationDto $location */ |
133
|
|
|
$location = $pit->poll(); |
134
|
|
|
|
135
|
|
|
if (!$location instanceof LocationDto) { |
|
|
|
|
136
|
|
|
$this->logger->debug( |
137
|
|
|
'No more vacancies to parse.', |
138
|
|
|
[ |
139
|
|
|
'source' => $sourceName, |
140
|
|
|
'input' => gettype($location), |
141
|
|
|
] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$scanner = $this->scannerPool->requireByLocation($location); |
148
|
|
|
|
149
|
|
|
$scanResult = $scanner->scan($location); |
150
|
|
|
|
151
|
|
|
$scanResultNormalized = $this->normalizer->normalize($scanResult); |
152
|
|
|
$this->logger->info('Vacancy parsed.', ['source' => $sourceName, 'data' => $scanResultNormalized]); |
153
|
|
|
|
154
|
|
|
$this->destination->offer($scanResult); |
155
|
|
|
|
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|