Total Complexity | 54 |
Total Lines | 324 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AlInterfaces 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 AlInterfaces, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class AlInterfaces |
||
26 | { |
||
27 | |||
28 | var $dir; // Directory with all core and external triggers files |
||
29 | |||
30 | /** |
||
31 | * @var string[] Error codes (or messages) |
||
32 | */ |
||
33 | public $errors = array(); |
||
34 | |||
35 | /** |
||
36 | * Constructor |
||
37 | * |
||
38 | * @param DoliDB $db Database handler |
||
|
|||
39 | */ |
||
40 | function __construct() |
||
41 | { |
||
42 | // Config::$dbEngine = $db; |
||
43 | } |
||
44 | |||
45 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
46 | /** |
||
47 | * Function called when a Dolibarr business event occurs |
||
48 | * This function call all qualified triggers. |
||
49 | * |
||
50 | * @param string $action Trigger event code |
||
51 | * @param object $object Objet concerned. Some context information may also be provided into array property object->context. |
||
52 | * @param User $user Objet user |
||
53 | * @param Lang $langs Objet lang |
||
54 | * @param Conf $conf Objet conf |
||
55 | * @return int Nb of triggers ran if no error, -Nb of triggers with errors otherwise. |
||
56 | */ |
||
57 | function run_triggers($action, $object/* , $user, $langs, $conf */) |
||
58 | { |
||
59 | $user = Globals::$user; |
||
60 | $langs = Globals::$langs; |
||
61 | $conf = Globals::$conf; |
||
62 | |||
63 | // phpcs:enable |
||
64 | // Check parameters |
||
65 | if (!is_object($object) || !is_object($conf)) { // Error |
||
66 | $this->error = 'function run_triggers called with wrong parameters action=' . $action . ' object=' . is_object($object) . ' user=' . is_object($user) . ' langs=' . is_object($langs) . ' conf=' . is_object($conf); |
||
67 | DolUtils::dol_syslog(get_class($this) . '::run_triggers ' . $this->error, LOG_ERR); |
||
68 | $this->errors[] = $this->error; |
||
69 | return -1; |
||
70 | } |
||
71 | if (!is_object($langs)) { // Warning |
||
72 | DolUtils::dol_syslog(get_class($this) . '::run_triggers was called with wrong parameters action=' . $action . ' object=' . is_object($object) . ' user=' . is_object($user) . ' langs=' . is_object($langs) . ' conf=' . is_object($conf), LOG_WARNING); |
||
73 | } |
||
74 | /* |
||
75 | if (!is_object($user)) { // Warning |
||
76 | DolUtils::dol_syslog(get_class($this) . '::run_triggers was called with wrong parameters action=' . $action . ' object=' . is_object($object) . ' user=' . is_object($user) . ' langs=' . is_object($langs) . ' conf=' . is_object($conf), LOG_WARNING); |
||
77 | global $db; |
||
78 | $user = new User($db); |
||
79 | } |
||
80 | */ |
||
81 | |||
82 | $nbfile = $nbtotal = $nbok = $nbko = 0; |
||
83 | |||
84 | $files = array(); |
||
85 | $modules = array(); |
||
86 | $orders = array(); |
||
87 | $i = 0; |
||
88 | |||
89 | // $dirtriggers = array_merge(array('/core/triggers'), Globals::$conf->modules_parts['triggers']); |
||
90 | $dirtriggers = array_merge(array(BASE_PATH . '/Helpers/triggers'), Globals::$conf->modules_parts['triggers']); |
||
91 | foreach ($dirtriggers as $reldir) { |
||
92 | //$dir = DolUtils::dol_buildpath($reldir, 0); |
||
93 | //$newdir = DolUtils::dol_osencode($dir); |
||
94 | $newdir = DolUtils::dol_buildpath($reldir, 0); |
||
95 | //print "xx".$dir;exit; |
||
96 | // Check if directory exists (we do not use dol_is_dir to avoir loading files.lib.php at each call) |
||
97 | if (!is_dir($newdir)) |
||
98 | continue; |
||
99 | |||
100 | $handle = opendir($newdir); |
||
101 | if (is_resource($handle)) { |
||
102 | while (($file = readdir($handle)) !== false) { |
||
103 | if (is_readable($newdir . "/" . $file) && preg_match('/^interface_([0-9]+)_([^_]+)_(.+)\.class\.php$/i', $file, $reg)) { |
||
104 | $part1 = $reg[1]; |
||
105 | $part2 = $reg[2]; |
||
106 | $part3 = $reg[3]; |
||
107 | |||
108 | $nbfile++; |
||
109 | |||
110 | // Check if trigger file is disabled by name |
||
111 | if (preg_match('/NORUN$/i', $file)) |
||
112 | continue; |
||
113 | // Check if trigger file is for a particular module |
||
114 | $qualified = true; |
||
115 | if (strtolower($reg[2]) != 'all') { |
||
116 | $module = preg_replace('/^mod/i', '', $reg[2]); |
||
117 | $constparam = 'MAIN_MODULE_' . strtoupper($module); |
||
118 | if (empty(Globals::$conf->global->$constparam)) |
||
119 | $qualified = false; |
||
120 | } |
||
121 | |||
122 | if (!$qualified) { |
||
123 | //DolUtils::dol_syslog(get_class($this)."::run_triggers action=".$action." Triggers for file '".$file."' need module to be enabled", LOG_DEBUG); |
||
124 | continue; |
||
125 | } |
||
126 | |||
127 | $modName = "Interface" . ucfirst($reg[3]); |
||
128 | //print "file=$file - modName=$modName\n"; |
||
129 | if (in_array($modName, $modules)) { // $modules = list of modName already loaded |
||
130 | $langs->load("errors"); |
||
131 | DolUtils::dol_syslog(get_class($this) . "::run_triggers action=" . $action . " " . $langs->trans("ErrorDuplicateTrigger", $newdir . "/" . $file, $fullpathfiles[$modName]), LOG_WARNING); |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | try { |
||
136 | //print 'Todo for '.$modName." : ".$newdir.'/'.$file."\n"; |
||
137 | include_once $newdir . '/' . $file; |
||
138 | //print 'Done for '.$modName."\n"; |
||
139 | } catch (Exception $e) { |
||
140 | DolUtils::dol_syslog('ko for ' . $modName . " " . $e->getMessage() . "\n", LOG_ERR); |
||
141 | } |
||
142 | |||
143 | $modules[$i] = $modName; |
||
144 | $files[$i] = $file; |
||
145 | $fullpathfiles[$modName] = $newdir . '/' . $file; |
||
146 | $orders[$i] = $part1 . '_' . $part2 . '_' . $part3; // Set sort criteria value |
||
147 | |||
148 | $i++; |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | asort($orders); |
||
155 | |||
156 | // Loop on each trigger |
||
157 | foreach ($orders as $key => $value) { |
||
158 | $modName = $modules[$key]; |
||
159 | if (empty($modName)) |
||
160 | continue; |
||
161 | |||
162 | $objMod = new $modName(); |
||
163 | if ($objMod) { |
||
164 | $result = 0; |
||
165 | |||
166 | if (method_exists($objMod, 'runTrigger')) { // New method to implement |
||
167 | //DolUtils::dol_syslog(get_class($this)."::run_triggers action=".$action." Launch runTrigger for file '".$files[$key]."'", LOG_DEBUG); |
||
168 | $result = $objMod->runTrigger($action, $object, $user, $langs, $conf); |
||
169 | } elseif (method_exists($objMod, 'run_trigger')) { // Deprecated method |
||
170 | DolUtils::dol_syslog(get_class($this) . "::run_triggers action=" . $action . " Launch old method run_trigger (rename your trigger into runTrigger) for file '" . $files[$key] . "'", LOG_WARNING); |
||
171 | $result = $objMod->run_trigger($action, $object, $user, $langs, $conf); |
||
172 | } else { |
||
173 | DolUtils::dol_syslog(get_class($this) . "::run_triggers action=" . $action . " A trigger was declared for class " . get_class($objMod) . " but method runTrigger was not found", LOG_ERR); |
||
174 | } |
||
175 | |||
176 | if ($result > 0) { |
||
177 | // Action OK |
||
178 | $nbtotal++; |
||
179 | $nbok++; |
||
180 | } |
||
181 | if ($result == 0) { |
||
182 | // Aucune action faite |
||
183 | $nbtotal++; |
||
184 | } |
||
185 | if ($result < 0) { |
||
186 | // Action KO |
||
187 | //DolUtils::dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($objMod->errors), LOG_ERR); |
||
188 | $nbtotal++; |
||
189 | $nbko++; |
||
190 | if (!empty($objMod->errors)) |
||
191 | $this->errors = array_merge($this->errors, $objMod->errors); |
||
192 | else if (!empty($objMod->error)) |
||
193 | $this->errors[] = $objMod->error; |
||
194 | //DolUtils::dol_syslog("Error in trigger ".$action." - Nb of error string returned = ".count($this->errors), LOG_ERR); |
||
195 | } |
||
196 | } |
||
197 | else { |
||
198 | DolUtils::dol_syslog(get_class($this) . "::run_triggers action=" . $action . " Failed to instantiate trigger for file '" . $files[$key] . "'", LOG_ERR); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | if ($nbko) { |
||
203 | DolUtils::dol_syslog(get_class($this) . "::run_triggers action=" . $action . " Files found: " . $nbfile . ", Files launched: " . $nbtotal . ", Done: " . $nbok . ", Failed: " . $nbko . " - Nb of error string returned in this->errors = " . count($this->errors), LOG_ERR); |
||
204 | return -$nbko; |
||
205 | } else { |
||
206 | //DolUtils::dol_syslog(get_class($this)."::run_triggers Files found: ".$nbfile.", Files launched: ".$nbtotal.", Done: ".$nbok.", Failed: ".$nbko, LOG_DEBUG); |
||
207 | return $nbok; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Return list of triggers. Function used by admin page htdoc/admin/triggers. |
||
213 | * List is sorted by trigger filename so by priority to run. |
||
214 | * |
||
215 | * @param array $forcedirtriggers null=All default directories. This parameter is used by modulebuilder module only. |
||
216 | * @return array Array list of triggers |
||
217 | */ |
||
218 | function getTriggersList($forcedirtriggers = null) |
||
349 | } |
||
350 | } |
||
351 |