Complex classes like ODT_Class 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ODT_Class, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ODT_Class extends Attachment_Class { |
||
23 | |||
24 | /** |
||
25 | * Le nom du modèle |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $model_name = '\eoxia\ODT_Model'; |
||
30 | |||
31 | /** |
||
32 | * Le type du post |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $type = 'attachment'; |
||
37 | |||
38 | /** |
||
39 | * Le type du post |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $base = 'eo-attachment'; |
||
44 | |||
45 | /** |
||
46 | * La clé principale pour post_meta |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $meta_key = 'eo_attachment'; |
||
51 | |||
52 | /** |
||
53 | * Nom de la taxonomy |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $attached_taxonomy_type = 'attachment_category'; |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Le nom pour le resgister post type |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $post_type_name = 'ODT'; |
||
66 | |||
67 | /** |
||
68 | * Utiles pour récupérer la clé unique |
||
69 | * |
||
70 | * @todo Rien à faire ici |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $identifier_helper = 'odt'; |
||
74 | |||
75 | /** |
||
76 | * Le chemin vers le modèle |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $model_path = ''; |
||
81 | |||
82 | /** |
||
83 | * Les types par défaut des modèles. |
||
84 | * |
||
85 | * @since 1.0.0 |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $default_types = array( 'model', 'default_model' ); |
||
90 | |||
91 | /** |
||
92 | * Le nom du modèle ODT. |
||
93 | * |
||
94 | * @since 1.0.0 |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $odt_name = ''; |
||
99 | |||
100 | protected $path = ''; |
||
101 | protected $url = ''; |
||
102 | |||
103 | /** |
||
104 | * Récupères le chemin vers le dossier frais-pro dans wp-content/uploads |
||
105 | * |
||
106 | * @param string $path_type (Optional) Le type de path. |
||
107 | * |
||
108 | * @return string Le chemin vers le document |
||
109 | */ |
||
110 | public function get_dir_path( $path_type = 'basedir' ) { |
||
111 | $upload_dir = wp_upload_dir(); |
||
112 | $response = str_replace( '\\', '/', $upload_dir[ $path_type ] ); |
||
113 | return $response; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Récupération de la liste des modèles de fichiers disponible pour un type d'élément |
||
118 | * |
||
119 | * @since 6.0.0 |
||
120 | * |
||
121 | * @param array $model_type Le type du document. |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function get_default_model( $model_type ) { |
||
126 | if ( 'zip' === $model_type ) { |
||
127 | return; |
||
128 | } |
||
129 | |||
130 | $response = array( |
||
131 | 'status' => true, |
||
132 | 'id' => null, |
||
133 | 'path' => str_replace( '\\', '/', $this->path . 'core/assets/document_template/' . $this->odt_name . '.odt' ), |
||
134 | 'url' => str_replace( '\\', '/', $this->url . 'core/assets/document_template/' . $this->odt_name . '.odt' ), |
||
135 | // translators: Pour exemple: Le modèle utilisé est: C:\wamp\www\wordpress\wp-content\plugins\digirisk-alpha\core\assets\document_template\document_unique.odt. |
||
136 | 'message' => sprintf( __( 'Le modèle utilisé est: %1$score/assets/document_template/%2$s.odt', 'digirisk' ), $this->path, $this->odt_name ), |
||
137 | ); |
||
138 | |||
139 | // Merge tous les types ensembles. |
||
140 | $types = array_merge( $this->default_types, (array) $model_type ); |
||
141 | |||
142 | // Préparation de la query pour récupérer le modèle par défaut selon $model_type. |
||
143 | $tax_query = array( |
||
144 | 'relation' => 'AND', |
||
145 | ); |
||
146 | |||
147 | if ( ! empty( $types ) ) { |
||
148 | foreach ( $types as $type ) { |
||
149 | $tax_query[] = array( |
||
150 | 'taxonomy' => $this->get_attached_taxonomy(), |
||
151 | 'field' => 'slug', |
||
152 | 'terms' => $type, |
||
153 | ); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | // Lances la Query pour récupérer le document par défaut selon $model_type. |
||
158 | $query = new \WP_Query( array( |
||
159 | 'fields' => 'ids', |
||
160 | 'post_status' => 'inherit', |
||
161 | 'posts_per_page' => 1, |
||
162 | 'tax_query' => $tax_query, |
||
163 | 'post_type' => 'attachment', |
||
164 | ) ); |
||
165 | |||
166 | // Récupères le document |
||
167 | if ( $query->have_posts() ) { |
||
168 | $upload_dir = wp_upload_dir(); |
||
169 | |||
170 | $model_id = $query->posts[0]; |
||
171 | $attachment_file_path = str_replace( '\\', '/', get_attached_file( $model_id ) ); |
||
172 | $response['id'] = $model_id; |
||
173 | $response['path'] = str_replace( '\\', '/', $attachment_file_path ); |
||
174 | $response['url'] = str_replace( str_replace( '\\', '/', $upload_dir['basedir'] ), str_replace( '\\', '/', $upload_dir['baseurl'] ), $attachment_file_path ); |
||
175 | |||
176 | // translators: Pour exemple: Le modèle utilisé est: C:\wamp\www\wordpress\wp-content\plugins\digirisk-alpha\core\assets\document_template\document_unique.odt. |
||
177 | $response['message'] = sprintf( __( 'Le modèle utilisé est: %1$s', 'digirisk' ), $attachment_file_path ); |
||
178 | } |
||
179 | |||
180 | if ( ! is_file( $response['path'] ) ) { |
||
181 | $response['status'] = false; |
||
182 | $response['message'] = 'Le modèle ' . $response['path'] . ' est introuvable.'; |
||
183 | } |
||
184 | |||
185 | return $response; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Récupération de la prochaine version pour un type de document pour le jour J |
||
190 | * |
||
191 | * @since 6.0.0 |
||
192 | * |
||
193 | * @param string $type Le type de document actuellement en cours de création. |
||
194 | * @param integer $element_id L'ID de l'élément. |
||
195 | * |
||
196 | * @return integer La version +1 du document actuellement en cours de création. |
||
197 | */ |
||
198 | public function get_revision( $type, $element_id ) { |
||
199 | global $wpdb; |
||
200 | |||
201 | // Récupération de la date courante. |
||
202 | $today = getdate(); |
||
203 | |||
204 | // Définition des paramètres de la requête de récupération des documents du type donné pour la date actuelle. |
||
205 | $args = array( |
||
206 | 'count' => true, |
||
207 | 'posts_per_page' => -1, |
||
208 | 'post_parent' => $element_id, |
||
209 | 'post_type' => $type, |
||
210 | 'post_status' => array( 'publish', 'inherit' ), |
||
211 | 'date_query' => array( |
||
212 | array( |
||
213 | 'year' => $today['year'], |
||
214 | 'month' => $today['mon'], |
||
215 | 'day' => $today['mday'], |
||
216 | ), |
||
217 | ), |
||
218 | ); |
||
219 | |||
220 | $document_revision = new \WP_Query( $args ); |
||
221 | return ( $document_revision->post_count + 1 ); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Enregistres les données du document. |
||
226 | * |
||
227 | * @since 1.1.0 |
||
228 | * |
||
229 | * @param mixed $parent_id L'ID où est attaché le document. |
||
230 | * @param array $document_meta Les métadonnées du document. |
||
231 | * @param array $args Arguments supplémentaires. |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function save_document_data( $parent_id, $document_meta, $args = array() ) { |
||
236 | $response = array(); |
||
237 | |||
238 | // Récupères le modèle a utiliser pour la futur génération de l'ODT. |
||
239 | $model_infos = $this->get_default_model( $this->get_type() ); |
||
240 | |||
241 | if ( ! $model_infos['status'] ) { |
||
242 | $response['message'] = $model_infos['message']; |
||
243 | $response['path'] = $model_infos['path']; |
||
244 | return $response; |
||
245 | } |
||
246 | |||
247 | // On met à jour les informations concernant le document dans la |
||
248 | // base de données. |
||
249 | $document_args = array( |
||
250 | 'model_id' => $model_infos['id'], |
||
251 | 'model_path' => $model_infos['path'], |
||
252 | 'parent_id' => $parent_id, |
||
253 | 'parent' => $args['parent'], |
||
254 | 'status' => 'inherit', |
||
255 | 'document_meta' => $document_meta, |
||
256 | ); |
||
257 | |||
258 | $document = $this->update( $document_args ); |
||
259 | wp_set_object_terms( $document->data['id'], array( $this->get_type(), 'printed' ), $this->attached_taxonomy_type ); |
||
260 | |||
261 | $response['document'] = $document; |
||
262 | |||
263 | return $response; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Création du document dans la base de données puis appel de la fonction de génération du fichier |
||
268 | * |
||
269 | * @since 1.0.0 |
||
270 | * |
||
271 | * @param object $element L'élément pour lequel il faut créer le document |
||
272 | * @param array $document_meta Les données a écrire dans le modèle de document |
||
273 | * |
||
274 | * @return object Le résultat de la création du document |
||
275 | */ |
||
276 | public function create_document( $document_id ) { |
||
277 | $response = array( |
||
278 | 'status' => true, |
||
279 | 'message' => '', |
||
280 | 'filename' => '', |
||
281 | 'path' => '', |
||
282 | ); |
||
283 | |||
284 | $document = $this->generate_document( $document_id ); |
||
285 | |||
286 | $file_info = $this->check_file( $document ); |
||
287 | |||
288 | if ( $file_info['exists'] ) { |
||
289 | $document->data['mime_type'] = $file_info['mime_type']['type']; |
||
290 | } |
||
291 | |||
292 | $document->data['file_generated'] = true; |
||
293 | |||
294 | $document = $this->update( $document->data ); |
||
295 | |||
296 | $response['document'] = $document; |
||
297 | |||
298 | return $response; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Vérification de l'existence d'un fichier à partir de la définition d'un document. |
||
303 | * |
||
304 | * 1- On remplace l'url du site "site_url( '/' )" par le chemin "ABSPATH" contenant les fichiers du site: on vérifie si le fichier existe. |
||
305 | * 2- Si le fichier n'existe pas: |
||
306 | * 2.a- On récupère la meta associée automatiquement par WordPress. |
||
307 | * 2.b- Si la méta n'est pas vide, on vérifie que sa valeur concaténée au chemin absolu des uploads "wp_upload_dir()" de WordPress soit bien un fichier |
||
308 | * |
||
309 | * @since 1.0.0 |
||
310 | * |
||
311 | * @param Document_Model $document La définition du document à vérifier. |
||
312 | * |
||
313 | * @return array Tableau avec le status d'existence du fichier (True/False) et le lien de téléchargement du fichier. |
||
314 | */ |
||
315 | public function check_file( $document ) { |
||
316 | // Définition des valeurs par défaut. |
||
317 | $file_check = array( |
||
318 | 'exists' => false, |
||
319 | 'path' => '', |
||
320 | 'mime_type' => '', |
||
321 | 'link' => '', |
||
322 | ); |
||
323 | |||
324 | if ( ! empty( $document->data['link'] ) ) { |
||
325 | $file_check['path'] = str_replace( site_url( '/' ), ABSPATH, $document->data['link'] ); |
||
326 | $file_check['link'] = $document->data['link']; |
||
327 | } |
||
328 | |||
329 | $upload_dir = wp_upload_dir(); |
||
330 | |||
331 | // Vérification principale. cf 1 ci-dessus. |
||
332 | if ( is_file( $file_check['path'] ) ) { |
||
333 | $file_check['exists'] = true; |
||
334 | } |
||
335 | |||
336 | // La vérification principale n'a pas fonctionnée. cf 2 ci-dessus. |
||
337 | if ( ! $file_check['exists'] && ! empty( $document->data['_wp_attached_file'] ) ) { |
||
338 | $file_check['path'] = $upload_dir['basedir'] . '/' . $document->data['_wp_attached_file']; |
||
339 | $file_check['link'] = $upload_dir['baseurl'] . '/' . $document->data['_wp_attached_file']; |
||
340 | if ( is_file( $file_check['path'] ) ) { |
||
341 | $file_check['exists'] = true; |
||
342 | } |
||
343 | } |
||
344 | |||
345 | // Si le fichier existe on récupère le mime type. |
||
346 | if ( $file_check['exists'] ) { |
||
347 | $file_check['mime_type'] = wp_check_filetype( $file_check['path'] ); |
||
348 | } |
||
349 | |||
350 | return $file_check; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Création d'un fichier odt a partir d'un modèle de document donné et d'un modèle de donnée |
||
355 | * |
||
356 | * @since 6.0.0 |
||
357 | * |
||
358 | * @param string $model_path Le chemin vers le fichier modèle a utiliser pour la génération. |
||
359 | * @param array $document_content Un tableau contenant le contenu du fichier à écrire selon l'élément en cours d'impression. |
||
360 | * @param string $document_name Le nom du document. |
||
361 | * |
||
362 | * array['status'] boolean True si tout s'est bien passé sinon false. |
||
363 | * array['message'] string Le message informatif de la méthode. |
||
364 | * array['path'] string Le chemin absolu vers le fichier. |
||
365 | * array['url'] string L'url vers le fichier. |
||
366 | * |
||
367 | * @return array (Voir au dessus). |
||
368 | */ |
||
369 | public function generate_document( $document_id ) { |
||
370 | $document = $this->get( array( 'id' => $document_id ), true ); |
||
371 | |||
372 | $response = array( |
||
373 | 'status' => false, |
||
374 | 'message' => '', |
||
375 | 'path' => '', |
||
376 | 'url' => '', |
||
377 | ); |
||
378 | |||
379 | if ( empty( $document->data['path'] ) ) { |
||
380 | $response['message'] = 'Document path est vide'; |
||
381 | return $response; |
||
382 | } |
||
383 | |||
384 | @ini_set( 'memory_limit', '256M' ); |
||
385 | |||
386 | |||
387 | require_once $this->path . '/core/external/odtPhpLibrary/odf.php'; |
||
388 | |||
389 | $upload_dir = wp_upload_dir(); |
||
390 | $directory = str_replace( '\\', '/', $upload_dir['basedir'] ); |
||
391 | |||
392 | $config = array( |
||
393 | 'PATH_TO_TMP' => $directory . '/tmp', |
||
394 | ); |
||
395 | if ( ! is_dir( $config['PATH_TO_TMP'] ) ) { |
||
396 | wp_mkdir_p( $config['PATH_TO_TMP'] ); |
||
397 | } |
||
398 | |||
399 | if ( ! is_file( $document->data['model_path'] ) ) { |
||
400 | $response['message'] = $document->data['model_path'] . ' est introuvable'; |
||
401 | return $response; |
||
402 | } |
||
403 | |||
404 | |||
405 | // On créé l'instance pour la génération du document odt. |
||
406 | $odf_php_lib = new \DigiOdf( $document->data['model_path'], $config ); |
||
407 | |||
408 | // Vérification de l'existence d'un contenu a écrire dans le document. |
||
409 | if ( ! empty( $document->data['document_meta'] ) ) { |
||
410 | // Lecture du contenu à écrire dans le document. |
||
411 | foreach ( $document->data['document_meta'] as $data_key => $data_value ) { |
||
412 | if ( is_array( $data_value ) && ! empty( $data_value['raw'] ) ) { |
||
413 | $data_value = $data_value['raw']; |
||
414 | } |
||
415 | |||
416 | $odf_php_lib = $this->set_document_meta( $data_key, $data_value, $odf_php_lib ); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | // Vérification de l'existence du dossier de destination. |
||
421 | if ( ! is_dir( dirname( $document->data['path'] ) ) ) { |
||
422 | wp_mkdir_p( dirname( $document->data['path'] ) ); |
||
423 | } |
||
424 | |||
425 | // Enregistrement du document sur le disque. |
||
426 | $odf_php_lib->saveToDisk( $document->data['path'] ); |
||
427 | |||
428 | if ( is_file( $document->data['path'] ) ) { |
||
429 | $response['status'] = true; |
||
430 | $response['path'] = $document->data['path']; |
||
431 | } |
||
432 | |||
433 | return $document; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Ecris dans le document ODT |
||
438 | * |
||
439 | * @since 6.0.0 |
||
440 | * |
||
441 | * @param string $data_key La clé dans le ODT. |
||
442 | * @param string $data_value La valeur de la clé. |
||
443 | * @param object $current_odf Le document courant. |
||
444 | * |
||
445 | * @return object Le document courant |
||
446 | */ |
||
447 | public function set_document_meta( $data_key, $data_value, $current_odf ) { |
||
448 | // Dans le cas où la donnée a écrire est une valeur "simple" (texte). |
||
449 | if ( ! is_array( $data_value ) ) { |
||
450 | $current_odf->setVars( $data_key, stripslashes( $data_value ), true, 'UTF-8' ); |
||
451 | } else if ( is_array( $data_value ) && isset( $data_value[ 'type' ] ) && !empty( $data_value[ 'type' ] ) ) { |
||
452 | switch ( $data_value[ 'type' ] ) { |
||
453 | |||
454 | case 'picture': |
||
455 | $current_odf->setImage( $data_key, $data_value[ 'value' ], ( !empty( $data_value[ 'option' ] ) && !empty( $data_value[ 'option' ][ 'size' ] ) ? $data_value[ 'option' ][ 'size' ] : 0 ) ); |
||
456 | break; |
||
457 | |||
458 | case 'segment': |
||
459 | $segment = $current_odf->setdigiSegment( $data_key ); |
||
460 | |||
461 | if ( $segment && is_array( $data_value[ 'value' ] ) ) { |
||
462 | foreach ( $data_value[ 'value' ] as $segment_detail ) { |
||
463 | foreach ( $segment_detail as $segment_detail_key => $segment_detail_value ) { |
||
464 | if ( is_array( $segment_detail_value ) && array_key_exists( 'type', $segment_detail_value ) && ( 'sub_segment' == $segment_detail_value[ 'type' ] ) ) { |
||
465 | foreach ( $segment_detail_value[ 'value' ] as $sub_segment_data ) { |
||
466 | foreach ( $sub_segment_data as $sub_segment_data_key => $sub_segment_data_value ) { |
||
467 | $segment->$segment_detail_key = $this->set_document_meta( $sub_segment_data_key, $sub_segment_data_value, $segment->$segment_detail_key ); |
||
468 | } |
||
469 | |||
470 | $segment->$segment_detail_key->merge(); |
||
471 | } |
||
472 | } |
||
473 | else { |
||
474 | $segment = $this->set_document_meta( $segment_detail_key, $segment_detail_value, $segment ); |
||
475 | } |
||
476 | |||
477 | } |
||
478 | |||
479 | $segment->merge(); |
||
480 | } |
||
481 | |||
482 | $current_odf->mergedigiSegment( $segment ); |
||
483 | } |
||
484 | unset( $segment ); |
||
485 | break; |
||
486 | } |
||
487 | } |
||
488 | |||
489 | return $current_odf; |
||
490 | } |
||
491 | } |
||
492 | } // End if(). |
||
493 |