Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Init_Util   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 131
Duplicated Lines 6.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 8
loc 131
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Fichier boot d'un plugin made Eoxia.
4
 *
5
 * @author Eoxia <[email protected]>
6
 * @since 0.1.0
7
 * @version 1.0.0
8
 * @copyright 2015-2018 Eoxia
9
 * @package EO_Framework\Core\Util
10
 */
11
12
namespace eoxia;
13
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( '\eoxia\Init_Util' ) ) {
19
20
	/**
21
	 * Cette classe initialise tous les fichiers config.json
22
	 */
23
	class Init_Util extends \eoxia\Singleton_Util {
24
		/**
25
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
26
		 *
27
		 * @since 0.1.0
28
		 * @version 1.0.0
29
		 *
30
		 * @return void
31
		 */
32
		protected function construct() {}
33
34
		/**
35
		 * Appelles les méthodes read_core_util_file_and_include et init_main_config ainsi que init_module
36
		 *
37
		 * @param string $path        Le chemin absolue vers le plugin.
38
		 * @param string $plugin_slug Le slug du plugin (Défini dans votre config.json principale).
39
		 *
40
		 * @return void
41
		 */
42
		public function exec( $path, $plugin_slug ) {
43
			self::read_core_util_file_and_include( $path, $plugin_slug );
44
			self::init_main_config( $path, $plugin_slug );
45
			self::init_external( $path, $plugin_slug );
46
			Config_Util::$init['main'] = new \stdClass();
47
			Config_Util::$init['main']->full_plugin_path = $path;
48
			self::init_module( $path, $plugin_slug );
49
		}
50
51
		/**
52
		 * Listes la liste des fichiers ".utils" dans le dossier ./core/external/wpeo_util/
53
		 *
54
		 * @since 0.1.0
55
		 * @version 1.0.0
56
		 *
57
		 * @param string $path        Le chemin du plugin.
58
		 * @param string $plugin_slug Le slug principale du plugin défini dans le fichier config.json principale.
59
		 *
60
		 * @return WP_Error|bool {
61
		 *                            WP_Error Si le module n'existe pas dans le tableau externals du fichier principale de config.json.
62
		 *                            WP_Error Si le fichier n'existe pas
63
		 *                            bool     Si aucune erreur s'est produite.
64
		 *}
65
		 */
66
		private function read_core_util_file_and_include( $path, $plugin_slug ) {
67
			$path_to_core_folder_util = $path . 'core/util/';
68
			if ( ! file_exists( $path_to_core_folder_util ) ) {
69
				return new \WP_Error( 'broke', __( 'Impossible de charger les fichiers .utils', $plugin_slug ) );
70
			}
71
72
			if ( ! is_dir( $path_to_core_folder_util ) ) {
73
				return new \WP_Error( 'broke', __( '$path_to_core_folder_util n\'est pas un dossier', $plugin_slug ) );
74
			}
75
76
			$list_file_name = scandir( $path_to_core_folder_util );
77
78
			if ( ! $list_file_name || ! is_array( $list_file_name ) ) {
79
				return new \WP_Error( 'broke', __( 'Impossible de charger les fichiers .utils', $plugin_slug ) );
80
			}
81
82
			if ( ! empty( $list_file_name ) ) {
83
				foreach ( $list_file_name as $file_name ) {
84
					if ( '.' !== $file_name && '..' !== $file_name && 'index.php' !== $file_name && '.git' !== $file_name && 'README.md' !== $file_name ) {
85
						$file_path = realpath( $path_to_core_folder_util . $file_name );
86
						require_once( $file_path );
87
					}
88
				}
89
			}
90
		}
91
92
		/**
93
		 * Appelle la méthode init_config avec le fichier digirisk.config.json
94
		 *
95
		 * @since 0.1.0
96
		 * @version 1.0.0
97
		 *
98
		 * @param string $path        Le chemin du plugin.
99
		 * @param string $plugin_slug Le slug principale du plugin défini dans le fichier config.json principale.
100
		 *
101
		 * @return void
102
		 */
103
		private function init_main_config( $path, $plugin_slug ) {
104
			$main_config_path = $plugin_slug . '.config.json';
105
			$init_status = \eoxia\Config_Util::g()->init_config( $path . $main_config_path );
106
107
			if ( \is_wp_error( $init_status ) ) {
108
				exit( $init_status->errors['broke'][0] );
109
			}
110
111
			if ( isset( Config_Util::$init[ $plugin_slug ] ) ) {
112
				Config_Util::$init[ $plugin_slug ]->path = $path;
113
			}
114
		}
115
116
		/**
117
		* Appel la méthode 'exec' de l'objet 'External_Util' afin d'initialiser les externals.
118
		*
119
		* @since 0.1.0
120
		* @version 1.0.0
121
		*
122
		* @param string $path        Le chemin du plugin.
123
		* @param string $plugin_slug Le slug principale du plugin défini dans le fichier config.json principale.
124
		*
125
		* @return void
126
		*/
127
		private function init_external( $path, $plugin_slug ) {
128
			if ( empty( Config_Util::$init['external'] ) ) {
129
				Config_Util::$init['external'] = new \stdClass();
130
			}
131
132
			\eoxia\External_Util::g()->exec( $path, $plugin_slug );
133
		}
134
135
		/**
136
		 * Appelle la méthode exec_module de \eoxia\Module_Util pour initialiser tous les modules
137
		 *
138
		 * @since 0.1.0
139
		 * @version 1.0.0
140
		 *
141
		 * @param string $path        Le chemin du plugin.
142
		 * @param string $plugin_slug Le slug principale du plugin défini dans le fichier config.json principale.
143
		 *
144
		 * @return void
145
		 */
146
		private function init_module( $path, $plugin_slug ) {
147
			$init_module_status = \eoxia\Module_Util::g()->exec_module( $path, $plugin_slug );
148
149
			if ( \is_wp_error( $init_module_status ) ) {
150
				exit( $init_module_status->errors['broke'][0] );
151
			}
152
		}
153
	}
154
} // End if().
155