ext   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 152
Duplicated Lines 32.89 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 50
loc 152
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A is_enableable() 0 18 3
A enable_step() 0 31 3
A disable_step() 25 25 2
A purge_step() 25 25 2
A notification_handler() 0 11 2

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
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
// this file is not really needed, when empty it can be omitted
12
// however you can override the default methods and add custom
13
// installation logic
14
15
namespace ernadoo\phpbbdirectory;
16
17
class ext extends \phpbb\extension\base
18
{
19
	/**
20
	* Enable extension if requirements are met
21
	*
22
	* @return bool
23
	* @aceess public
24
	*/
25
	public function is_enableable()
26
	{
27
		$php_ini = $this->container->get('php_ini');
28
29
		// Check phpbb version
30
		if (!phpbb_version_compare(PHPBB_VERSION, '3.2.1', '>='))
31
		{
32
			return false;
33
		}
34
35
		if (!file_exists($this->extension_path. 'vendor/autoload.php'))
36
		{
37
			return false;
38
		}
39
40
		// Check for url_fopen
41
		return (bool) $php_ini->getBool('allow_url_fopen');
42
	}
43
44
	/**
45
	* Single enable step that installs any included migrations
46
	*
47
	* @param mixed $old_state State returned by previous call of this method
48
	* @return mixed Returns false after last step, otherwise temporary state
49
	*/
50
	public function enable_step($old_state)
51
	{
52
		switch ($old_state)
53
		{
54
			case '': // Empty means nothing has run yet
55
56
				// Enable notifications
57
				return $this->notification_handler('enable', array(
58
					'ernadoo.phpbbdirectory.notification.type.directory_website',
59
					'ernadoo.phpbbdirectory.notification.type.directory_website_approved',
60
					'ernadoo.phpbbdirectory.notification.type.directory_website_disapproved',
61
					'ernadoo.phpbbdirectory.notification.type.directory_website_error_cron',
62
					'ernadoo.phpbbdirectory.notification.type.directory_website_in_queue',
63
				));
64
65
			break;
66
67
			default:
68
69
				if (!class_exists('\E1379\SpeakingUrl\SpeakingUrl'))
70
				{
71
					// When migration is executed, phpBB doesn't know yet extension dependencies, so we need to manually include autoload file
72
					require($this->extension_path. 'vendor/autoload.php');
73
				}
74
75
				// Run parent enable step method
76
				return parent::enable_step($old_state);
77
78
			break;
79
		}
80
	}
81
82
	/**
83
	* Single disable step that does nothing
84
	*
85
	* @param mixed $old_state State returned by previous call of this method
86
	* @return mixed Returns false after last step, otherwise temporary state
87
	*/
88 View Code Duplication
	public function disable_step($old_state)
89
	{
90
		switch ($old_state)
91
		{
92
			case '': // Empty means nothing has run yet
93
94
				// Disable notifications
95
				return $this->notification_handler('disable', array(
96
					'ernadoo.phpbbdirectory.notification.type.directory_website',
97
					'ernadoo.phpbbdirectory.notification.type.directory_website_approved',
98
					'ernadoo.phpbbdirectory.notification.type.directory_website_disapproved',
99
					'ernadoo.phpbbdirectory.notification.type.directory_website_error_cron',
100
					'ernadoo.phpbbdirectory.notification.type.directory_website_in_queue',
101
				));
102
103
			break;
104
105
			default:
106
107
				// Run parent disable step method
108
				return parent::disable_step($old_state);
109
110
			break;
111
		}
112
	}
113
114
	/**
115
	* Single purge step that reverts any included and installed migrations
116
	*
117
	* @param mixed $old_state State returned by previous call of this method
118
	* @return mixed Returns false after last step, otherwise temporary state
119
	*/
120 View Code Duplication
	public function purge_step($old_state)
121
	{
122
		switch ($old_state)
123
		{
124
			case '': // Empty means nothing has run yet
125
126
				// Purge notifications
127
				return $this->notification_handler('purge', array(
128
					'ernadoo.phpbbdirectory.notification.type.directory_website',
129
					'ernadoo.phpbbdirectory.notification.type.directory_website_approved',
130
					'ernadoo.phpbbdirectory.notification.type.directory_website_disapproved',
131
					'ernadoo.phpbbdirectory.notification.type.directory_website_error_cron',
132
					'ernadoo.phpbbdirectory.notification.type.directory_website_in_queue',
133
				));
134
135
			break;
136
137
			default:
138
139
				// Run parent purge step method
140
				return parent::purge_step($old_state);
141
142
			break;
143
		}
144
	}
145
146
	/**
147
	* Notification handler to call notification enable/disable/purge steps
148
	*
149
	* @author VSEphpbb (Matt Friedman)
150
	* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
151
	* @license GNU General Public License, version 2 (GPL-2.0)
152
	* @param string $step The step (enable, disable, purge)
153
	* @param array $notification_types The notification type names
154
	* @return string Return notifications as temporary state
155
	* @access protected
156
	*/
157
	protected function notification_handler($step, $notification_types)
158
	{
159
		$phpbb_notifications = $this->container->get('notification_manager');
160
161
		foreach ($notification_types as $notification_type)
162
		{
163
			call_user_func(array($phpbb_notifications, $step . '_notifications'), $notification_type);
164
		}
165
166
		return 'notifications';
167
	}
168
}
169