Total Complexity | 55 |
Total Lines | 330 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CompleteProcess 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 CompleteProcess, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class CompleteProcess |
||
27 | { |
||
28 | /** |
||
29 | * Exclusion requête BnF/Google/etc |
||
30 | * Format EAN ou ISBN10 sans tiret. |
||
31 | */ |
||
32 | const ISBN_EAN_SKIP |
||
33 | = [ |
||
34 | '9782918758440', // Profils de lignes du réseau ferré français vol.2 |
||
35 | '9782918758341', // Profils de lignes du réseau ferré français vol.1 |
||
36 | ]; |
||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | public $verbose = false; |
||
41 | |||
42 | /** |
||
43 | * @var QueueInterface |
||
44 | */ |
||
45 | private $queueAdapter; |
||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $raw = ''; |
||
50 | private $page; // article title |
||
51 | |||
52 | private $log = []; |
||
53 | private $notCosmetic = false; |
||
54 | private $major = false; |
||
55 | /** |
||
56 | * @var OuvrageTemplate |
||
57 | */ |
||
58 | private $ouvrage; |
||
59 | |||
60 | public function __construct(QueueInterface $queueAdapter, ?bool $verbose = false) |
||
61 | { |
||
62 | $this->queueAdapter = $queueAdapter; |
||
63 | $this->verbose = (bool)$verbose; |
||
64 | } |
||
65 | |||
66 | public function run(?int $limit = 10000) |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get array (title+raw strings) to complete from AMQP queue, SQL Select or file reading. |
||
137 | * |
||
138 | * @return string|null |
||
139 | * @throws \Exception |
||
140 | */ |
||
141 | private function getNewRow2Complete(): ?array |
||
142 | { |
||
143 | $row = $this->queueAdapter->getNewRaw(); |
||
144 | if (empty($row) || empty($row['raw'])) { |
||
145 | echo "STOP: no more queue to process \n"; |
||
146 | throw new \Exception('no more queue to process'); |
||
147 | } |
||
148 | |||
149 | return $row; |
||
1 ignored issue
–
show
|
|||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param string $isbn |
||
154 | * @param string|null $isbn10 |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | private function isIsbnSkipped(string $isbn, ?string $isbn10 = null): bool |
||
159 | { |
||
160 | if (in_array(str_replace('-', '', $isbn), self::ISBN_EAN_SKIP) |
||
161 | || ($isbn10 !== null |
||
162 | && in_array(str_replace('-', '', $isbn10), self::ISBN_EAN_SKIP)) |
||
163 | ) { |
||
164 | return true; |
||
165 | } |
||
166 | |||
167 | return false; |
||
168 | } |
||
169 | |||
170 | private function onlineIsbnSearch(string $isbn, ?string $isbn10 = null) |
||
171 | { |
||
172 | if ($this->isIsbnSkipped($isbn, $isbn10)) { |
||
173 | echo "*** SKIP THAT ISBN ***\n"; |
||
174 | |||
175 | // Vérifier logique return |
||
176 | return; |
||
177 | } |
||
178 | |||
179 | online: |
||
180 | if ($this->verbose) { |
||
181 | echo "sleep 10...\n"; |
||
182 | } |
||
183 | sleep(10); |
||
184 | |||
185 | try { |
||
186 | if ($this->verbose) { |
||
187 | dump('BIBLIO NAT FRANCE...'); |
||
188 | } |
||
189 | // BnF sait pas trouver un vieux livre (10) d'après ISBN-13... FACEPALM ! |
||
190 | if ($isbn10) { |
||
191 | $bnfOuvrage = OuvrageFactory::BnfFromIsbn($isbn10); |
||
192 | sleep(2); |
||
193 | } |
||
194 | if (!$isbn10 || empty($bnfOuvrage) || empty($bnfOuvrage->getParam('titre'))) { |
||
195 | $bnfOuvrage = OuvrageFactory::BnfFromIsbn($isbn); |
||
196 | } |
||
197 | if (isset($bnfOuvrage) and $bnfOuvrage instanceof OuvrageTemplate) { |
||
198 | $this->completeOuvrage($bnfOuvrage); |
||
199 | |||
200 | // Wikidata requests from $infos (ISBN/ISNI) |
||
201 | if (!empty($bnfOuvrage->getInfos())) { |
||
202 | if ($this->verbose) { |
||
203 | dump('WIKIDATA...'); |
||
204 | } |
||
205 | // TODO move to factory |
||
206 | $wikidataAdapter = new WikidataAdapter( |
||
207 | new Client(['timeout' => 5, 'headers' => ['User-Agent' => getenv('USER_AGENT')]]) |
||
208 | ); |
||
209 | $wdComplete = new Wikidata2Ouvrage($wikidataAdapter, clone $bnfOuvrage, $this->page); |
||
210 | $this->completeOuvrage($wdComplete->getOuvrage()); |
||
211 | } |
||
212 | } |
||
213 | } catch (Throwable $e) { |
||
214 | echo sprintf( |
||
215 | "*** ERREUR BnF Isbn Search %s %s %s \n", |
||
216 | $e->getMessage(), |
||
217 | $e->getFile(), |
||
218 | $e->getLine() |
||
219 | ); |
||
220 | } |
||
221 | |||
222 | if (!isset($bnfOuvrage) || !$this->skipGoogle($bnfOuvrage)) { |
||
223 | try { |
||
224 | if ($this->verbose) { |
||
225 | dump('GOOGLE...'); |
||
226 | } |
||
227 | $googleOuvrage = OuvrageFactory::GoogleFromIsbn($isbn); |
||
228 | $this->completeOuvrage($googleOuvrage); |
||
229 | } catch (Throwable $e) { |
||
230 | echo "*** ERREUR GOOGLE Isbn Search ***".$e->getMessage()."\n"; |
||
231 | if( strpos($e->getMessage(), 'Could not resolve host: www.googleapis.com') === false) { |
||
232 | throw $e; |
||
233 | } |
||
234 | unset($e); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | if (!isset($bnfOuvrage) && !isset($googleOuvrage)) { |
||
239 | try { |
||
240 | if ($this->verbose) { |
||
241 | dump('OpenLibrary...'); |
||
242 | } |
||
243 | $openLibraryOuvrage = OuvrageFactory::OpenLibraryFromIsbn($isbn); |
||
244 | if (!empty($openLibraryOuvrage)) { |
||
245 | $this->completeOuvrage($openLibraryOuvrage); |
||
246 | } |
||
247 | } catch (Throwable $e) { |
||
248 | echo '**** ERREUR OpenLibrary Isbn Search'; |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // private function onlineQuerySearch(string $query) |
||
254 | // { |
||
255 | // echo "sleep 40..."; |
||
256 | // sleep(20); |
||
257 | // onlineQuerySearch: |
||
258 | // |
||
259 | // try { |
||
260 | // dump('GOOGLE SEARCH...'); |
||
261 | // // $googleOuvrage = OuvrageFactory::GoogleFromIsbn($isbn); |
||
262 | // $adapter = new GoogleBooksAdapter(); |
||
263 | // $data = $adapter->search('blabla'); |
||
264 | // dump($data); |
||
265 | // //die; |
||
266 | // // return $import->getOuvrage(); |
||
267 | // // $this->completeOuvrage($googleOuvrage); |
||
268 | // } catch (Throwable $e) { |
||
269 | // echo "*** ERREUR GOOGLE QuerySearch *** ".$e->getMessage()."\n"; |
||
270 | // echo "sleep 30min"; |
||
271 | // sleep(60 * 30); |
||
272 | // echo "Wake up\n"; |
||
273 | // goto onlineQuerySearch; |
||
274 | // } |
||
275 | // } |
||
276 | |||
277 | private function completeOuvrage(OuvrageTemplate $onlineOuvrage) |
||
278 | { |
||
279 | if ($this->verbose) { |
||
280 | dump($onlineOuvrage->serialize(true)); |
||
281 | } |
||
282 | $optimizer = new OuvrageOptimize($onlineOuvrage, $this->page); |
||
283 | $onlineOptimized = ($optimizer)->doTasks()->getOuvrage(); |
||
284 | |||
285 | $completer = new OuvrageComplete($this->ouvrage, $onlineOptimized); |
||
286 | $this->ouvrage = $completer->getResult(); |
||
287 | |||
288 | // todo move that optimizing in OuvrageComplete ? |
||
289 | $optimizer = new OuvrageOptimize($this->ouvrage, $this->page); |
||
290 | $this->ouvrage = $optimizer->doTasks()->getOuvrage(); |
||
291 | |||
292 | if ($this->verbose) { |
||
293 | dump($completer->getLog()); |
||
294 | } |
||
295 | if ($completer->major) { |
||
296 | $this->major = true; |
||
297 | } |
||
298 | $this->notCosmetic = ($completer->notCosmetic || $this->notCosmetic); |
||
299 | $this->log = array_merge($this->log, $completer->getLog()); |
||
300 | unset($optimizer); |
||
301 | unset($completer); |
||
302 | } |
||
303 | |||
304 | private function sendCompleted() |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Final serialization of the completed OuvrageTemplate. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | private function serializeFinalOpti(): string |
||
343 | } |
||
344 | |||
345 | private function skipGoogle($bnfOuvrage): bool |
||
346 | { |
||
358 |