Passed
Push — scrutinizer-code-quality ( 09f5a1...c4c5fb )
by Adam
56:05 queued 14:08
created

LogicHook::LogicHook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 2 Features 1
Metric Value
cc 2
eloc 7
c 2
b 2
f 1
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
1
<?php
2
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
/*********************************************************************************
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
42
/**
43
 * Predefined logic hooks
44
 * after_ui_frame
45
 * after_ui_footer
46
 * after_save
47
 * before_save
48
 * before_retrieve
49
 * after_retrieve
50
 * process_record
51
 * before_delete
52
 * after_delete
53
 * before_restore
54
 * after_restore
55
 * server_roundtrip
56
 * before_logout
57
 * after_logout
58
 * before_login
59
 * after_login
60
 * login_failed
61
 * after_session_start
62
 * after_entry_point
63
 *
64
 * @api
65
 */
66
class LogicHook{
67
68
	var $bean = null;
69
70 153
	public function __construct(){
71 153
	}
72
73
    /**
74
     * @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead
75
     */
76
    public function LogicHook(){
77
        $deprecatedMessage = 'PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code';
78
        if(isset($GLOBALS['log'])) {
79
            $GLOBALS['log']->deprecated($deprecatedMessage);
80
        }
81
        else {
82
            trigger_error($deprecatedMessage, E_USER_DEPRECATED);
83
        }
84
        self::__construct();
85
    }
86
87
88
	/**
89
	 * Static Function which returns and instance of LogicHook
90
	 *
91
	 * @return unknown
92
	 */
93 3
	static function initialize(){
94 3
		if(empty($GLOBALS['logic_hook']))
95
			$GLOBALS['logic_hook'] = new LogicHook();
96 3
		return $GLOBALS['logic_hook'];
97
	}
98
99 146
	function setBean($bean){
100 146
		$this->bean = $bean;
101 146
		return $this;
102
	}
103
104
	protected $hook_map = array();
105
	protected $hookscan = array();
106
107 2
	public function getHooksMap()
108
	{
109 2
	    return $this->hook_map;
110
	}
111
112 2
	public function getHooksList()
113
	{
114 2
	    return $this->hookscan;
115
	}
116
117 1
    public function scanHooksDir($extpath)
118
    {
119 1
		if(is_dir($extpath)){
120 1
		    $dir = dir($extpath);
121 1
			while($entry = $dir->read()){
122 1
				if($entry != '.' && $entry != '..' && strtolower(substr($entry, -4)) == ".php" && is_file($extpath.'/'.$entry)) {
123 1
				    unset($hook_array);
124 1
                    include($extpath.'/'.$entry);
125 1
                    if(!empty($hook_array)) {
126 1
                        foreach($hook_array as $type => $hookg) {
127 1
                            foreach($hookg as $index => $hook) {
128 1
                                $this->hookscan[$type][] = $hook;
129 1
                                $idx = count($this->hookscan[$type])-1;
130 1
                                $this->hook_map[$type][$idx] = array("file" => $extpath.'/'.$entry, "index" => $index);
131
                            }
132
                        }
133
                    }
134
				}
135
			}
136
		}
137 1
    }
138
139
	protected static $hooks = array();
140
141 1
        static public function refreshHooks() {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
142 1
            self::$hooks = array();
143 1
        }
144
145 51
	public function loadHooks($module_dir)
146
	{
147 51
        $hook_array = array();
148 51
	    if(!empty($module_dir)) {
149 49
	        $custom = "custom/modules/$module_dir";
150
	    } else {
151 3
	        $custom = "custom/modules";
152
	    }
153 51
		if(file_exists("$custom/logic_hooks.php")){
154 14
            if(isset($GLOBALS['log'])){
155 14
	    	    $GLOBALS['log']->debug('Including module specific hook file for '.$custom);
156
            }
157 14
		    include("$custom/logic_hooks.php");
158
		}
159 51
		if(empty($module_dir)) {
160 3
		    $custom = "custom/application";
161
		}
162 51
		if(file_exists("$custom/Ext/LogicHooks/logichooks.ext.php")) {
163 3
            if(isset($GLOBALS['log'])){
164 3
			    $GLOBALS['log']->debug('Including Ext hook file for '.$custom);
165
            }
166 3
			include("$custom/Ext/LogicHooks/logichooks.ext.php");
167
		}
168 51
		return $hook_array;
169
	}
170
171 148
	public function getHooks($module_dir, $refresh = false)
172
	{
173 148
	    if($refresh || !isset(self::$hooks[$module_dir])) {
174 49
	        self::$hooks[$module_dir] = $this->loadHooks($module_dir);
175
	    }
176 148
	    return self::$hooks[$module_dir];
177
	}
178
179
	/**
180
	 * Provide a means for developers to create upgrade safe business logic hooks.
181
	 * If the bean is null, then we assume this call was not made from a SugarBean Object and
182
	 * therefore we do not pass it to the method call.
183
	 *
184
	 * @param string $module_dir
185
	 * @param string $event
186
	 * @param array $arguments
187
	 * @param SugarBean $bean
0 ignored issues
show
Bug introduced by
There is no parameter named $bean. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
188
	 */
189 147
	function call_custom_logic($module_dir, $event, $arguments = null){
190
		// declare the hook array variable, it will be defined in the included file.
191 147
		$hook_array = null;
192 147
        if(isset($GLOBALS['log'])){
193 147
            $GLOBALS['log']->debug("Hook called: $module_dir::$event");
194
        }
195 147
		if(!empty($module_dir)){
196
			// This will load an array of the hooks to process
197 144
			$hooks = $this->getHooks($module_dir);
198 144
			if(!empty($hooks)) {
199 80
			    $this->process_hooks($hooks, $event, $arguments);
0 ignored issues
show
Bug introduced by
It seems like $arguments defined by parameter $arguments on line 189 can also be of type null; however, LogicHook::process_hooks() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
200
			}
201
		}
202 147
		$hooks = $this->getHooks('');
203 147
		if(!empty($hooks)) {
204 147
		    $this->process_hooks($hooks, $event, $arguments);
0 ignored issues
show
Bug introduced by
It seems like $arguments defined by parameter $arguments on line 189 can also be of type null; however, LogicHook::process_hooks() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
205
		}
206 147
	}
207
208
	/**
209
	 * This is called from call_custom_logic and actually performs the action as defined in the
210
	 * logic hook. If the bean is null, then we assume this call was not made from a SugarBean Object and
211
	 * therefore we do not pass it to the method call.
212
	 *
213
	 * @param array $hook_array
214
	 * @param string $event
215
	 * @param array $arguments
216
	 * @param SugarBean $bean
0 ignored issues
show
Bug introduced by
There is no parameter named $bean. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
217
	 */
218 148
	function process_hooks($hook_array, $event, $arguments){
219
		// Now iterate through the array for the appropriate hook
220 148
		if(!empty($hook_array[$event])){
221
222
			// Apply sorting to the hooks using the sort index.
223
			// Hooks with matching sort indexes will be processed in no particular order.
224 74
			$sorted_indexes = array();
225 74
			foreach($hook_array[$event] as $idx => $hook_details)
226
			{
227 74
				$order_idx = $hook_details[0];
228 74
				$sorted_indexes[$idx] = $order_idx;
229
			}
230 74
			asort($sorted_indexes);
231
232 74
			$process_order = array_keys($sorted_indexes);
233
234 74
			foreach($process_order as $hook_index){
235 74
				$hook_details = $hook_array[$event][$hook_index];
236 74
				if(!file_exists($hook_details[2])){
237
                    if(isset($GLOBALS['log'])){
238
					    $GLOBALS['log']->error('Unable to load custom logic file: '.$hook_details[2]);
239
                    }
240
					continue;
241
				}
242 74
				include_once($hook_details[2]);
243 74
				$hook_class = $hook_details[3];
244 74
				$hook_function = $hook_details[4];
245
246
				// Make a static call to the function of the specified class
247
				//TODO Make a factory for these classes.  Cache instances accross uses
248 74
				if($hook_class == $hook_function){
249
                    if(isset($GLOBALS['log'])){
250
					    $GLOBALS['log']->debug('Creating new instance of hook class '.$hook_class.' with parameters');
251
                    }
252
					if(!is_null($this->bean))
253
						$class = new $hook_class($this->bean, $event, $arguments);
254
					else
255
						$class = new $hook_class($event, $arguments);
256
				}else{
257 74
                    if(isset($GLOBALS['log'])){
258 74
					    $GLOBALS['log']->debug('Creating new instance of hook class '.$hook_class.' without parameters');
259
                    }
260 74
					$class = new $hook_class();
261 74
					if(!is_null($this->bean))
262 73
						$class->$hook_function($this->bean, $event, $arguments);
263
					else
264 74
						$class->$hook_function($event, $arguments);
265
				}
266
			}
267
		}
268 148
	}
269
}
270
?>
271