Total Complexity | 42 |
Total Lines | 315 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Scrapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Scrapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Scrapper extends Communicator |
||
22 | { |
||
23 | private const ERROR_INVALID_SPECIAL_KEY = '!#ERR - S.Key not valid!'; |
||
24 | private const KEY_PREFIX = ConfigProvider::SPECIAL_KEY_PREFIX; |
||
25 | private const KEY_TARGET = self::KEY_PREFIX . 'target'; |
||
26 | private const KEY_ID = self::KEY_PREFIX . 'id'; |
||
27 | private const KEY_SERP_RESULT = self::KEY_PREFIX . 'serp_result'; |
||
28 | private const ENCODE_CHARSET = 'UTF-8'; |
||
29 | |||
30 | /** |
||
31 | * @var array[] |
||
32 | */ |
||
33 | private array $raw; |
||
34 | private LoggerInterface $logger; |
||
35 | private int $newScrapsCount; |
||
36 | private Collection $relatedObjects; |
||
37 | private Scanner $scanner; |
||
38 | |||
39 | /** |
||
40 | * Scraps found so far. |
||
41 | */ |
||
42 | private Collection $scraps; |
||
43 | |||
44 | /** |
||
45 | * Scrapper constructor. |
||
46 | */ |
||
47 | public function __construct( |
||
48 | LoggerInterface $logger, |
||
49 | ?Command $callingCommand = null, |
||
50 | string $hashAlgorithm = self::HASH_ALGORITHM, |
||
51 | int $verbosity = self::VERBOSITY_LOW |
||
52 | ) { |
||
53 | parent::__construct($callingCommand); |
||
54 | |||
55 | $this->raw = []; |
||
56 | $this->logger = $logger; |
||
57 | $this->scanner = new Scanner(); |
||
58 | $this->hashAlgorithm = $hashAlgorithm; |
||
59 | $this->scraps = collect([]); |
||
60 | $this->relatedObjects = collect([]); |
||
61 | $this->newScrapsCount = 0; |
||
62 | $this->verbosity = $verbosity; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Collect scrap array from crawler using markup. |
||
67 | * |
||
68 | * @throws JsonException |
||
69 | */ |
||
70 | public function collect(TitleLink $titleLink, Target $target, Crawler $crawler): void |
||
71 | { |
||
72 | $markup = $target->getMarkup(); |
||
73 | $data = $this->initializeData( |
||
74 | $titleLink, |
||
75 | $target, |
||
76 | $crawler, |
||
77 | $markup[TargetKey::special(TargetKey::MARKUP_INSIDE)] ?? [] |
||
78 | ); |
||
79 | $data = $this->preprocess($data, $target); |
||
80 | |||
81 | if (!$this->verifyData($data, $target)) { |
||
82 | return; |
||
83 | } |
||
84 | |||
85 | $data = $this->finalizeData($data, $target); |
||
86 | |||
87 | $this->raw[$data[self::KEY_ID]] = $data; |
||
88 | $this->scraps->push($this->buildScrapFromData($data)); |
||
89 | |||
90 | // feedback |
||
91 | $this->tell( |
||
92 | FormattedMessage::get(FormattedMessage::SCRAP_GATHERED, $data[self::KEY_ID]) |
||
93 | . ($this->verbosity >= self::VERBOSITY_HIGH ? '-- ' . json_encode($data, JSON_THROW_ON_ERROR, 512) : null) |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | public function convertScraps(bool $convertDuplicates = false, bool $storeRelatedReferences = false): void |
||
98 | { |
||
99 | $this->scraps->map( |
||
100 | function (Scrap $scrap) use ($convertDuplicates, $storeRelatedReferences) { |
||
101 | try { |
||
102 | $relatedObject = $scrap->convert($convertDuplicates, $storeRelatedReferences); |
||
103 | if ($relatedObject !== null) { |
||
104 | $this->relatedObjects->push($relatedObject); |
||
105 | } |
||
106 | } catch (QueryException $e) { |
||
107 | $this->logger->warning($e, ['scrap' => $scrap]); |
||
108 | } |
||
109 | } |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | public function getNewScrapsCount(): int |
||
114 | { |
||
115 | return $this->newScrapsCount; |
||
116 | } |
||
117 | |||
118 | public function getScraps(): Collection |
||
119 | { |
||
120 | return $this->scraps; |
||
121 | } |
||
122 | |||
123 | public function saveScraps(): void |
||
124 | { |
||
125 | $this->scraps->map( |
||
126 | function (Scrap $scrap): void { |
||
127 | try { |
||
128 | $scrap->save(); |
||
129 | } catch (QueryException $e) { |
||
130 | $errorMessage = FormattedMessage::get( |
||
131 | FormattedMessage::SCRAP_SAVE_EXCEPTION, |
||
132 | $scrap->hash, |
||
133 | $e->getMessage() |
||
134 | ); |
||
135 | if ($this->verbosity >= self::VERBOSITY_HIGH) { |
||
136 | $errorMessage .= ' -- ' . $scrap->toJson(); |
||
137 | } |
||
138 | |||
139 | $this->tell($errorMessage); |
||
140 | $this->logger->error($errorMessage); |
||
141 | } |
||
142 | } |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Retrieve related objects which are a result of scrap conversion. |
||
148 | */ |
||
149 | public function getRelatedObjects(): Collection |
||
150 | { |
||
151 | return $this->relatedObjects; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Initialize data. |
||
156 | * |
||
157 | * @noinspection PhpTooManyParametersInspection |
||
158 | */ |
||
159 | private function initializeData( |
||
160 | TitleLink $titleLink, |
||
161 | Target $target, |
||
162 | Crawler $crawler, |
||
163 | array $markupOverride = [] |
||
164 | ): array { |
||
165 | $markup = !empty($markupOverride) ? $markupOverride : $target->getMarkup(); |
||
166 | |||
167 | $data[TargetKey::TITLE] = $titleLink->getTitle(); |
||
|
|||
168 | $data[TargetKey::special(TargetKey::LINK)] = $titleLink->getLink(); |
||
169 | $data[TargetKey::special(TargetKey::POSITION)] = $target->getCursor(); |
||
170 | $data[TargetKey::special(TargetKey::SOURCE)] = $titleLink->getLink(); |
||
171 | $data[TargetKey::special(TargetKey::MODEL)] = $target->getModel(); |
||
172 | |||
173 | // build initial scrap data from markup and dissect |
||
174 | foreach ($markup as $attr => $path) { |
||
175 | if (ConfigProvider::isSpecialKey($path)) { |
||
176 | // path is special key, use special key value from scrap |
||
177 | $data[$attr] = !empty($data[$path]) ? $data[$path] : self::ERROR_INVALID_SPECIAL_KEY; |
||
178 | } elseif (!ConfigProvider::isSpecialKey($attr)) { |
||
179 | try { |
||
180 | $attrCrawler = $crawler->filter($path); |
||
181 | $data[$attr] = $attr === TargetKey::TITLE ? $attrCrawler->text() : $attrCrawler->html(); |
||
182 | |||
183 | // split single attributes into multiple based on regex |
||
184 | if (!empty($target->getDissect()[$attr])) { |
||
185 | $dissectMap = $target->getDissect()[$attr]; |
||
186 | |||
187 | // check _retain meta property |
||
188 | // to determine whether details should be left in source attribute after extraction |
||
189 | $retain = empty($dissectMap[TargetKey::special(TargetKey::RETAIN)]) |
||
190 | ? false |
||
191 | : $dissectMap[TargetKey::special(TargetKey::RETAIN)]; |
||
192 | unset($dissectMap[TargetKey::special(TargetKey::RETAIN)]); |
||
193 | |||
194 | // Extract details into scrap |
||
195 | $data = array_merge($data, Scanner::pluckDetails($data[$attr], $dissectMap, $retain)); |
||
196 | |||
197 | // unset dissectMap |
||
198 | unset($dissectMap); |
||
199 | } |
||
200 | } catch (InvalidArgumentException $e) { |
||
201 | $exMessage = FormattedMessage::get( |
||
202 | FormattedMessage::EXCEPTION_THROWN_FOR_ATTRIBUTE, |
||
203 | $attr, |
||
204 | $target->getName(), |
||
205 | $e->getMessage() |
||
206 | ); |
||
207 | $this->tell($exMessage); |
||
208 | $this->logger->warning($exMessage, [$markup[$attr]]); |
||
209 | } |
||
210 | } |
||
211 | unset($path); |
||
212 | } |
||
213 | |||
214 | return $data; |
||
215 | } |
||
216 | |||
217 | private function preprocess(array $data, Target $target): array |
||
218 | { |
||
219 | // preprocess and remap scrap data parts |
||
220 | foreach ($data as $attr => $value) { |
||
221 | $data[$attr] = $this->encodeAttribute($data, $attr); |
||
222 | |||
223 | // preprocess |
||
224 | if (!empty($target->getPreprocess()[$attr])) { |
||
225 | $preprocess = $target->getPreprocess()[$attr]; |
||
226 | // check for optional third parameter of array, which indicates that callable method needs an instance |
||
227 | if (is_array($preprocess) && isset($preprocess[2])) { |
||
228 | // if callable needs instance, resolve object |
||
229 | if ($preprocess[2]) { |
||
230 | $preprocess[0] = resolve($preprocess[0]); |
||
231 | } |
||
232 | unset($preprocess[2]); |
||
233 | } |
||
234 | // if preprocess is callable call it on attribute value |
||
235 | if (is_callable($preprocess)) { |
||
236 | try { |
||
237 | $data[$attr] = $preprocess($data[$attr]); |
||
238 | } catch (Exception $e) { |
||
239 | $this->logger->error($e); |
||
240 | } |
||
241 | } else { |
||
242 | $this->logger->warning( |
||
243 | FormattedMessage::get( |
||
244 | FormattedMessage::PREPROCESS_NOT_CALLABLE, |
||
245 | $attr, |
||
246 | $target->getName() |
||
247 | ) |
||
248 | ); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | // remap entity attribute name if specified |
||
253 | if (!empty($target->getRemap()[$attr])) { |
||
254 | $newAttrName = $target->getRemap()[$attr]; |
||
255 | $data[$newAttrName] = !empty($data[$attr]) ? $data[$attr] : null; |
||
256 | |||
257 | if ($attr !== TargetKey::TITLE) { |
||
258 | unset($data[$attr]); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | return $data; |
||
264 | } |
||
265 | |||
266 | private function encodeAttribute(array $data, string $attr): string |
||
279 | ); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @throws JsonException |
||
284 | */ |
||
285 | private function verifyData(array $data, Target $target): bool |
||
286 | { |
||
287 | if ($this->scanner->hasBadWords($data, $target->getBadWords())) { |
||
288 | $badWordMessage = sprintf( |
||
289 | FormattedMessage::SCRAP_CONTAINS_BAD_WORD, |
||
290 | json_encode($data, JSON_THROW_ON_ERROR, 512) |
||
291 | ); |
||
292 | $this->logger->notice($badWordMessage); |
||
293 | if ($this->verbosity >= self::VERBOSITY_HIGH) { |
||
294 | $this->tell($badWordMessage); |
||
295 | } |
||
296 | |||
297 | return false; |
||
298 | } |
||
299 | |||
300 | return true; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Finalize scrap. |
||
305 | */ |
||
306 | private function finalizeData(array $data, Target $target): array |
||
307 | { |
||
308 | $data[self::KEY_ID] = hash($this->hashAlgorithm, json_encode($data, JSON_THROW_ON_ERROR, 512)); |
||
309 | $data[self::KEY_SERP_RESULT] = $target->isSearchEngineRequestPages(); |
||
310 | $data[self::KEY_TARGET] = $target->getName(); |
||
311 | |||
312 | ksort($data); |
||
313 | |||
314 | return $data; |
||
315 | } |
||
316 | |||
317 | private function buildScrapFromData(array $data): Scrap |
||
336 | } |
||
337 | } |
||
338 |