Total Complexity | 49 |
Total Lines | 281 |
Duplicated Lines | 0 % |
Changes | 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 |
||
24 | class CompleteProcess |
||
25 | { |
||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | public $verbose = false; |
||
30 | |||
31 | /** |
||
32 | * @var QueueInterface |
||
33 | */ |
||
34 | private $queueAdapter; |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $raw = ''; |
||
39 | private $page; // article title |
||
40 | |||
41 | private $log = []; |
||
42 | private $notCosmetic = false; |
||
43 | private $major = false; |
||
44 | /** |
||
45 | * @var OuvrageTemplate |
||
46 | */ |
||
47 | private $ouvrage; |
||
48 | |||
49 | |||
50 | public function __construct(QueueInterface $queueAdapter, ?bool $verbose = false) |
||
51 | { |
||
52 | $this->queueAdapter = $queueAdapter; |
||
53 | $this->verbose = (bool)$verbose; |
||
54 | } |
||
55 | |||
56 | public function run(?int $limit = 10000) |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get array (title+raw strings) to complete from AMQP queue, SQL Select or file reading. |
||
127 | * |
||
128 | * @return string|null |
||
129 | * @throws \Exception |
||
130 | */ |
||
131 | private function getNewRow2Complete(): ?array |
||
132 | { |
||
133 | $row = $this->queueAdapter->getNewRaw(); |
||
134 | if (empty($row) || empty($row['raw'])) { |
||
135 | echo "STOP: no more queue to process \n"; |
||
136 | throw new \Exception('no more queue to process'); |
||
137 | } |
||
138 | |||
139 | return $row; |
||
1 ignored issue
–
show
|
|||
140 | } |
||
141 | |||
142 | private function onlineIsbnSearch(string $isbn, ?string $isbn10 = null) |
||
143 | { |
||
144 | online: |
||
145 | if ($this->verbose) { |
||
146 | echo "sleep 10...\n"; |
||
147 | } |
||
148 | sleep(10); |
||
149 | |||
150 | try { |
||
151 | if ($this->verbose) { |
||
152 | dump('BIBLIO NAT FRANCE...'); |
||
153 | } |
||
154 | // BnF sait pas trouver un vieux livre (10) d'après ISBN-13... FACEPALM ! |
||
155 | if ($isbn10) { |
||
156 | $bnfOuvrage = OuvrageFactory::BnfFromIsbn($isbn10); |
||
157 | sleep(2); |
||
158 | } |
||
159 | if (!$isbn10 || empty($bnfOuvrage) || empty($bnfOuvrage->getParam('titre'))) { |
||
160 | $bnfOuvrage = OuvrageFactory::BnfFromIsbn($isbn); |
||
161 | } |
||
162 | if (isset($bnfOuvrage) and $bnfOuvrage instanceof OuvrageTemplate) { |
||
163 | $this->completeOuvrage($bnfOuvrage); |
||
164 | |||
165 | // Wikidata requests from $infos (ISBN/ISNI) |
||
166 | if (!empty($bnfOuvrage->getInfos())) { |
||
167 | if ($this->verbose) { |
||
168 | dump('WIKIDATA...'); |
||
169 | } |
||
170 | $wdComplete = new Wikidata2Ouvrage(clone $bnfOuvrage, $this->page); |
||
171 | $this->completeOuvrage($wdComplete->getOuvrage()); |
||
172 | } |
||
173 | } |
||
174 | } catch (Throwable $e) { |
||
175 | echo sprintf( |
||
176 | "*** ERREUR BnF Isbn Search %s %s %s \n", |
||
177 | $e->getMessage(), |
||
178 | $e->getFile(), |
||
179 | $e->getLine() |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | if (!isset($bnfOuvrage) || !$this->skipGoogle($bnfOuvrage)) { |
||
184 | try { |
||
185 | if ($this->verbose) { |
||
186 | dump('GOOGLE...'); |
||
187 | } |
||
188 | $googleOuvrage = OuvrageFactory::GoogleFromIsbn($isbn); |
||
189 | $this->completeOuvrage($googleOuvrage); |
||
190 | } catch (Throwable $e) { |
||
191 | echo "*** ERREUR GOOGLE Isbn Search ***".$e->getMessage()."\n"; |
||
192 | throw $e; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | if (!isset($bnfOuvrage) && !isset($googleOuvrage)) { |
||
197 | try { |
||
198 | if ($this->verbose) { |
||
199 | dump('OpenLibrary...'); |
||
200 | } |
||
201 | $openLibraryOuvrage = OuvrageFactory::OpenLibraryFromIsbn($isbn); |
||
202 | if (!empty($openLibraryOuvrage)) { |
||
203 | $this->completeOuvrage($openLibraryOuvrage); |
||
204 | } |
||
205 | } catch (Throwable $e) { |
||
206 | echo '**** ERREUR OpenLibrary Isbn Search'; |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | // private function onlineQuerySearch(string $query) |
||
212 | // { |
||
213 | // echo "sleep 40..."; |
||
214 | // sleep(20); |
||
215 | // onlineQuerySearch: |
||
216 | // |
||
217 | // try { |
||
218 | // dump('GOOGLE SEARCH...'); |
||
219 | // // $googleOuvrage = OuvrageFactory::GoogleFromIsbn($isbn); |
||
220 | // $adapter = new GoogleBooksAdapter(); |
||
221 | // $data = $adapter->search('blabla'); |
||
222 | // dump($data); |
||
223 | // //die; |
||
224 | // // return $import->getOuvrage(); |
||
225 | // // $this->completeOuvrage($googleOuvrage); |
||
226 | // } catch (Throwable $e) { |
||
227 | // echo "*** ERREUR GOOGLE QuerySearch *** ".$e->getMessage()."\n"; |
||
228 | // echo "sleep 30min"; |
||
229 | // sleep(60 * 30); |
||
230 | // echo "Wake up\n"; |
||
231 | // goto onlineQuerySearch; |
||
232 | // } |
||
233 | // } |
||
234 | |||
235 | private function completeOuvrage(OuvrageTemplate $onlineOuvrage) |
||
236 | { |
||
237 | if ($this->verbose) { |
||
238 | dump($onlineOuvrage->serialize(true)); |
||
239 | } |
||
240 | $optimizer = new OuvrageOptimize($onlineOuvrage, $this->page); |
||
241 | $onlineOptimized = ($optimizer)->doTasks()->getOuvrage(); |
||
242 | |||
243 | $completer = new OuvrageComplete($this->ouvrage, $onlineOptimized); |
||
244 | $this->ouvrage = $completer->getResult(); |
||
245 | if ($this->verbose) { |
||
246 | dump($completer->getLog()); |
||
247 | } |
||
248 | if ($completer->major) { |
||
249 | $this->major = true; |
||
250 | } |
||
251 | $this->notCosmetic = ($completer->notCosmetic || $this->notCosmetic); |
||
252 | $this->log = array_merge($this->log, $completer->getLog()); |
||
253 | unset($optimizer); |
||
254 | unset($completer); |
||
255 | } |
||
256 | |||
257 | private function sendCompleted() |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Final serialization of the completed OuvrageTemplate. |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | private function serializeFinalOpti(): string |
||
292 | } |
||
293 | |||
294 | private function skipGoogle($bnfOuvrage): bool |
||
295 | { |
||
307 |