Completed
Push — master ( 153c83...604bee )
by Jonathan
03:10
created

HookProvider::getPossibleHooks()   D

Complexity

Conditions 10
Paths 2

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
rs 4.983
cc 10
eloc 28
nc 2
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
 * 
5
 * @package MyArtJaub\Webtrees
6
 * @subpackage Hook
7
 * @author Jonathan Jaubart <[email protected]>
8
 * @copyright Copyright (c) 2011-2016, Jonathan Jaubart
9
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
10
 */
11
namespace MyArtJaub\Webtrees\Hook;
12
13
use Fisharebest\Webtrees\Auth;
14
use Fisharebest\Webtrees\Database;
15
use Fisharebest\Webtrees\Module;
16
use MyArtJaub\Webtrees\Constants;
17
use MyArtJaub\Webtrees\Module\ModuleManager;
18
19
/**
20
 * Provider for hooks. 
21
 * 
22
 * Provide access to hooks.
23
 */
24
class HookProvider implements HookProviderInterface {
25
26
	/**
27
	 * Default priority to be used for hooks without specified priority. 
28
	 * The default 99 is a low priority.
29
	 * @var int DEFAULT_PRIORITY
30
	 */
31
	const DEFAULT_PRIORITY = 99;
32
33
	/**
34
	 * @var HookProviderInterface $instance Singleton pattern instance
35
	 */
36
	protected static $instance = null;
37
	
38
39
	/**
40
	 * Returns the *HookProvider* instance of this class.
41
	 *
42
	 * @return HookProviderInterface The *Singleton* instance.
43
	 */
44
	public static function getInstance()
45
	{
46
	    if (null === static::$instance) {
47
	        static::$instance = new static();
48
	    }
49
	
50
	    return static::$instance;
51
	}
52
	
53
	/**
54
	 * {@inheritDoc}
55
	 * @see \MyArtJaub\Webtrees\Hook\HookProviderInterface::get()
56
	 */
57
	public function get($hook_function, $hook_context = null) {
58
	    return new Hook($hook_function, $hook_context);
59
	}
60
	
61
	/**
62
	 * {@inheritDoc}
63
	 * @see \MyArtJaub\Webtrees\Hook\HookProviderInterface::isModuleOperational()
64
	 */
65
	public function isModuleOperational() {
66
		return ModuleManager::getInstance()->isOperational(Constants::MODULE_MAJ_HOOKS_NAME);
67
	}
68
	
69
	/**
70
	 * {@inheritDoc}
71
	 * @see \MyArtJaub\Webtrees\Hook\HookProviderInterface::getPossibleHooks()
72
	 */
73
	public function getPossibleHooks() {
74
		static $hooks=null;
75
		if ($hooks === null) {
76
		    $hooks = array();
77
		    
78
		    // Cannot use the same logic as the core Module loading,
79
		    // as this forces a new include of the module.php file.
80
		    // This causes issue when classes are defined in this file.
81
		    // Cannot use Module::getActiveModules as well, as this is private.
82
		    $module_names = Database::prepare(
83
		        'SELECT SQL_CACHE module_name FROM `##module`'
84
		    )->fetchOneColumn();
85
		    
86
		    foreach($module_names as $module_name) {
87
		        $module = Module::getModuleByName($module_name);
88
		        
89
		        if($module instanceof HookSubscriberInterface){
90
					$subscribedhooks = $module->getSubscribedHooks();
91
					if(is_array($subscribedhooks)){
92
						foreach($subscribedhooks as $key => $value){
93
							if(is_int($key)) {
94
								$hook_item = $value;
95
								$priority = self::DEFAULT_PRIORITY;
96
							}
97
							else{
98
								$hook_item = explode('#', $key, 2);
99
								$priority = $value;
100
							}
101
							if($hook_item && count($hook_item) == 2){
102
								$hook_func = $hook_item[0];
103
								$hook_cont = $hook_item[1];
104
							}
105
							else{
106
								$hook_func = $hook_item[0];
107
								$hook_cont = 'all';
108
							}
109
							if(method_exists($module, $hook_func)){
110
								$hooks[$module->getName().'#'.$hook_func.'#'.$hook_cont]=$priority;
111
							}
112
						}
113
					}
114
		        }
115
			}
116
		}
117
		return $hooks;
118
	}
119
	
120
	/**
121
	 * {@inheritDoc}
122
	 * @see \MyArtJaub\Webtrees\Hook\HookProviderInterface::getRawInstalledHooks()
123
	 */
124
	public function getRawInstalledHooks(){
125
		if(self::isModuleOperational()){
126
			return Database::prepare(
127
					"SELECT majh_id AS id, majh_module_name AS module, majh_hook_function AS hook, majh_hook_context as context, majh_module_priority AS priority,  majh_status AS status".
128
					" FROM `##maj_hooks`".
129
					" ORDER BY hook ASC, status ASC, priority ASC, module ASC"
130
					)->execute()->fetchAll();
131
		}
132
		return array();
133
	}
134
	
135
	/**
136
	 * {@inheritDoc}
137
	 * @see \MyArtJaub\Webtrees\Hook\HookProviderInterface::getInstalledHooks()
138
	 */
139
	public function getInstalledHooks(){
140
		static $installedhooks =null;
141
		if($installedhooks===null){
142
			$dbhooks=self::getRawInstalledHooks();
143
			foreach($dbhooks as $dbhook){
144
				$installedhooks[($dbhook->module).'#'.($dbhook->hook).'#'.($dbhook->context)] = array('id' => $dbhook->id, 'status' => $dbhook->status, 'priority' => $dbhook->priority);
145
			}
146
		}
147
		return $installedhooks;
148
	}
149
	
150
	/**
151
	 * {@inheritDoc}
152
	 * @see \MyArtJaub\Webtrees\Hook\HookProviderInterface::updateHooks()
153
	 */
154
	public function updateHooks() {
155
	    
156
	    if(Auth::isAdmin()){
157
	        $ihooks = self::getInstalledHooks();
158
	        $phooks = self::getPossibleHooks();
159
	        	
160
	        // Insert hooks not existing yet in the DB
161
	        if($phooks !== null){
162
	            foreach($phooks as $phook => $priority){
163
	                $array_hook = explode('#', $phook);
164
	                if($ihooks === null || !array_key_exists($phook, $ihooks)){
165
	                    $chook = new Hook($array_hook[1], $array_hook[2]);
166
	                    $chook->subscribe($array_hook[0]);
167
	                    $chook->setPriority($array_hook[0], $priority);
168
	                }
169
	            }
170
	        }
171
	        	
172
	        //Remove hooks not existing any more in the file system
173
	        if($ihooks !== null){
174
	            foreach(array_keys($ihooks) as $ihook){
175
	                $array_hook = explode('#', $ihook);
176
	                if($phooks === null || !array_key_exists($ihook, $phooks)){
177
	                    $chook = new Hook($array_hook[1], $array_hook[2]);
178
	                    $chook->remove($array_hook[0]);
179
	                }
180
	            }
181
	        }
182
	    }
183
	}
184
	
185
}