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