| Total Complexity | 55 |
| Total Lines | 331 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OuvrageCompleteWorker 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 OuvrageCompleteWorker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class OuvrageCompleteWorker |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Exclusion requête BnF/Google/etc |
||
| 34 | * Format EAN ou ISBN10 sans tiret. |
||
| 35 | */ |
||
| 36 | const ISBN_EAN_SKIP |
||
| 37 | = [ |
||
| 38 | '9782918758440', // Profils de lignes du réseau ferré français vol.2 |
||
| 39 | '9782918758341', // Profils de lignes du réseau ferré français vol.1 |
||
| 40 | ]; |
||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | public $verbose = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var QueueInterface |
||
| 48 | */ |
||
| 49 | private $queueAdapter; |
||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $raw = ''; |
||
| 54 | private $page; // article title |
||
| 55 | |||
| 56 | private $log = []; |
||
| 57 | private $notCosmetic = false; |
||
| 58 | private $major = false; |
||
| 59 | /** |
||
| 60 | * @var OuvrageTemplate |
||
| 61 | */ |
||
| 62 | private $ouvrage; |
||
| 63 | |||
| 64 | public function __construct(QueueInterface $queueAdapter, ?bool $verbose = false) |
||
| 65 | { |
||
| 66 | $this->queueAdapter = $queueAdapter; |
||
| 67 | $this->verbose = (bool)$verbose; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function run(?int $limit = 10000) |
||
| 71 | { |
||
| 72 | $memory = new Memory(); |
||
| 73 | while ($limit > 0) { |
||
| 74 | $limit--; |
||
| 75 | sleep(1); |
||
| 76 | $row = $this->getNewRow2Complete(); |
||
| 77 | $this->raw = $row['raw']; |
||
| 78 | $this->page = $row['page']; |
||
| 79 | |||
| 80 | echo sprintf( |
||
| 81 | "-------------------------------\n%s [%s]\n%s\n%s\n", |
||
| 82 | date("Y-m-d H:i:s"), |
||
| 83 | WikiBotConfig::getGitVersion() ?? '', |
||
| 84 | $this->page, |
||
| 85 | $this->raw |
||
| 86 | ); |
||
| 87 | if ($this->verbose) { |
||
| 88 | $memory->echoMemory(true); |
||
| 89 | } |
||
| 90 | |||
| 91 | // initialise variables |
||
| 92 | $this->log = []; |
||
| 93 | $this->ouvrage = null; |
||
| 94 | $this->notCosmetic = false; |
||
| 95 | $this->major = false; |
||
| 96 | |||
| 97 | |||
| 98 | try { |
||
| 99 | $parse = TemplateParser::parseAllTemplateByName('ouvrage', $this->raw); |
||
| 100 | $origin = $parse['ouvrage'][0]['model'] ?? null; |
||
| 101 | } catch (Throwable $e) { |
||
| 102 | echo sprintf("*** ERREUR impossible de transformer en modèle %s \n", $this->raw); |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!$origin instanceof OuvrageTemplate) { |
||
| 107 | echo sprintf("*** ERREUR impossible de transformer en modèle %s \n", $this->raw); |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Final optimizing (with online predictions) |
||
| 112 | $optimizer = new OuvrageOptimize($origin, $this->page); |
||
| 113 | $optimizer->doTasks(); |
||
| 114 | $this->ouvrage = $optimizer->getOuvrage(); |
||
| 115 | $this->log = array_merge($this->log, $optimizer->getLog()); |
||
| 116 | $this->notCosmetic = ($optimizer->notCosmetic || $this->notCosmetic); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * RECHERCHE ONLINE |
||
| 120 | */ |
||
| 121 | $isbn = $origin->getParam('isbn') ?? null; // avant mise en forme EAN>ISBN |
||
| 122 | $isbn10 = $origin->getParam('isbn2') ?? $origin->getParam('isbn10') ?? null; |
||
| 123 | if (!empty($isbn) |
||
| 124 | && !$origin->hasParamValue('isbn invalide') |
||
| 125 | && !$origin->hasParamValue('isbn erroné') |
||
| 126 | ) { |
||
| 127 | $this->onlineIsbnSearch($isbn, $isbn10); |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->sendCompleted(); |
||
| 131 | unset($optimizer); |
||
| 132 | unset($parse); |
||
| 133 | unset($origin); |
||
| 134 | } // END WHILE |
||
| 135 | |||
| 136 | return true; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get array (title+raw strings) to complete from AMQP queue, SQL Select or file reading. |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | * @throws Exception |
||
| 144 | */ |
||
| 145 | private function getNewRow2Complete(): array |
||
| 146 | { |
||
| 147 | $row = $this->queueAdapter->getNewRaw(); |
||
| 148 | if (empty($row) || empty($row['raw'])) { |
||
| 149 | echo "STOP: no more queue to process \n"; |
||
| 150 | throw new Exception('no more queue to process'); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $row; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $isbn |
||
| 158 | * @param string|null $isbn10 |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | private function isIsbnSkipped(string $isbn, ?string $isbn10 = null): bool |
||
| 163 | { |
||
| 164 | if (in_array(str_replace('-', '', $isbn), self::ISBN_EAN_SKIP) |
||
| 165 | || ($isbn10 !== null |
||
| 166 | && in_array(str_replace('-', '', $isbn10), self::ISBN_EAN_SKIP)) |
||
| 167 | ) { |
||
| 168 | return true; |
||
| 169 | } |
||
| 170 | |||
| 171 | return false; |
||
| 172 | } |
||
| 173 | |||
| 174 | private function onlineIsbnSearch(string $isbn, ?string $isbn10 = null) |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | // private function onlineQuerySearch(string $query) |
||
| 259 | // { |
||
| 260 | // echo "sleep 40..."; |
||
| 261 | // sleep(20); |
||
| 262 | // onlineQuerySearch: |
||
| 263 | // |
||
| 264 | // try { |
||
| 265 | // dump('GOOGLE SEARCH...'); |
||
| 266 | // // $googleOuvrage = OuvrageFactory::GoogleFromIsbn($isbn); |
||
| 267 | // $adapter = new GoogleBooksAdapter(); |
||
| 268 | // $data = $adapter->search('blabla'); |
||
| 269 | // dump($data); |
||
| 270 | // //die; |
||
| 271 | // // return $import->getOuvrage(); |
||
| 272 | // // $this->completeOuvrage($googleOuvrage); |
||
| 273 | // } catch (Throwable $e) { |
||
| 274 | // echo "*** ERREUR GOOGLE QuerySearch *** ".$e->getMessage()."\n"; |
||
| 275 | // echo "sleep 30min"; |
||
| 276 | // sleep(60 * 30); |
||
| 277 | // echo "Wake up\n"; |
||
| 278 | // goto onlineQuerySearch; |
||
| 279 | // } |
||
| 280 | // } |
||
| 281 | |||
| 282 | private function completeOuvrage(OuvrageTemplate $onlineOuvrage) |
||
| 283 | { |
||
| 284 | if ($this->verbose) { |
||
| 285 | dump($onlineOuvrage->serialize(true)); |
||
| 286 | } |
||
| 287 | $optimizer = new OuvrageOptimize($onlineOuvrage, $this->page); |
||
| 288 | $onlineOptimized = ($optimizer)->doTasks()->getOuvrage(); |
||
| 289 | |||
| 290 | $completer = new OuvrageComplete($this->ouvrage, $onlineOptimized); |
||
| 291 | $this->ouvrage = $completer->getResult(); |
||
| 292 | |||
| 293 | // todo move that optimizing in OuvrageComplete ? |
||
| 294 | $optimizer = new OuvrageOptimize($this->ouvrage, $this->page); |
||
| 295 | $this->ouvrage = $optimizer->doTasks()->getOuvrage(); |
||
| 296 | |||
| 297 | if ($this->verbose) { |
||
| 298 | dump($completer->getLog()); |
||
| 299 | } |
||
| 300 | if ($completer->major) { |
||
| 301 | $this->major = true; |
||
| 302 | } |
||
| 303 | $this->notCosmetic = ($completer->notCosmetic || $this->notCosmetic); |
||
| 304 | $this->log = array_merge($this->log, $completer->getLog()); |
||
| 305 | unset($optimizer); |
||
| 306 | unset($completer); |
||
| 307 | } |
||
| 308 | |||
| 309 | private function sendCompleted() |
||
| 310 | { |
||
| 311 | $isbn13 = $this->ouvrage->getParam('isbn') ?? null; |
||
| 312 | |||
| 313 | $finalData = [ |
||
| 314 | // 'page' => |
||
| 315 | 'raw' => $this->raw, |
||
| 316 | 'opti' => $this->serializeFinalOpti(), |
||
| 317 | 'optidate' => date("Y-m-d H:i:s"), |
||
| 318 | 'modifs' => mb_substr(implode(',', $this->log), 0, 250), |
||
| 319 | 'notcosmetic' => ($this->notCosmetic) ? 1 : 0, |
||
| 320 | 'major' => ($this->major) ? 1 : 0, |
||
| 321 | 'isbn' => substr($isbn13, 0, 20), |
||
| 322 | 'version' => WikiBotConfig::getGitVersion() ?? null, |
||
| 323 | ]; |
||
| 324 | if ($this->verbose) { |
||
| 325 | dump($finalData); |
||
| 326 | } |
||
| 327 | // Json ? |
||
| 328 | $result = $this->queueAdapter->sendCompletedData($finalData); |
||
| 329 | |||
| 330 | dump($result); // bool |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Final serialization of the completed OuvrageTemplate. |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | private function serializeFinalOpti(): string |
||
| 348 | } |
||
| 349 | |||
| 350 | private function skipGoogle($bnfOuvrage): bool |
||
| 361 | } |
||
| 362 | } |
||
| 363 |