ext   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 20
c 6
b 0
f 0
dl 0
loc 116
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A is_enableable() 0 5 2
A enable_step() 0 10 2
A notification_handler() 0 11 2
A purge_step() 0 10 2
A disable_step() 0 10 2
A notification_types() 0 6 1
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde;
12
13
/**
14
 * Extension class for custom enable/disable/purge actions
15
 *
16
 * NOTE TO EXTENSION DEVELOPERS:
17
 * Normally it is not necessary to define any functions inside the ext class below.
18
 * The ext class may contain special (un)installation commands in the methods
19
 * enable_step(), disable_step() and purge_step(). As it is, these methods are defined
20
 * in phpbb_extension_base, which this class extends, but you can overwrite them to
21
 * give special instructions for those cases.
22
 */
23
class ext extends \phpbb\extension\base
24
{
25
	/**
26
	 * Check whether or not the extension can be enabled.
27
	 * The current phpBB version should meet or exceed
28
	 * the minimum version required by this extension:
29
	 *
30
	 * Requires phpBB 3.3.11 and PHP 7.2.0
31
	 *
32
	 * @return bool
33
	 * @access public
34
	 */
35
	public function is_enableable()
36
	{
37
		$config = $this->container->get('config');
38
39
		return phpbb_version_compare($config['version'], '3.3.11', '>=') && PHP_VERSION_ID >= 70200;
40
	}
41
42
	/**
43
	 * Overwrite enable_step to enable extension notifications before any included migrations are installed.
44
	 *
45
	 * @param mixed $old_state State returned by previous call of this method
46
	 *
47
	 * @return bool|string Returns false after last step, otherwise temporary state
48
	 * @access public
49
	 */
50
	public function enable_step($old_state)
51
	{
52
		// Empty means nothing has run yet
53
		if ($old_state == '')
54
		{
55
			// Enable notifications
56
			return $this->notification_handler('enable', $this->notification_types());
57
		}
58
		// Run parent enable step method
59
		return parent::enable_step($old_state);
60
	}
61
62
	/**
63
	 * Overwrite disable_step to disable extension notifications before the extension is disabled.
64
	 *
65
	 * @param mixed $old_state State returned by previous call of this method
66
	 *
67
	 * @return false|string Returns false after last step, otherwise temporary state
68
	 * @access public
69
	 */
70
	public function disable_step($old_state)
71
	{
72
		// Empty means nothing has run yet
73
		if ($old_state == '')
74
		{
75
			// Disable notifications
76
			return $this->notification_handler('disable', $this->notification_types());
77
		}
78
		// Run parent disable step method
79
		return parent::disable_step($old_state);
80
	}
81
82
	/**
83
	 * Overwrite purge_step to purge extension notifications before any included and installed migrations are reverted.
84
	 *
85
	 * @param mixed $old_state State returned by previous call of this method
86
	 *
87
	 * @return bool|string Returns false after last step, otherwise temporary state
88
	 * @access public
89
	 */
90
	public function purge_step($old_state)
91
	{
92
		// Empty means nothing has run yet
93
		if ($old_state == '')
94
		{
95
			// Purge notifications
96
			return $this->notification_handler('purge', $this->notification_types());
97
		}
98
		// Run parent purge step method
99
		return parent::purge_step($old_state);
100
	}
101
102
	/**
103
	 * Notification handler to call notification enable/disable/purge steps
104
	 *
105
	 * @param string $step               The step (enable, disable, purge)
106
	 * @param array  $notification_types The notification type names
107
	 *
108
	 * @return string Return notifications as temporary state
109
	 * @access        protected
110
	 * @author        VSEphpbb (Matt Friedman)
111
	 * @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
112
	 * @license       GNU General Public License, version 2 (GPL-2.0)
113
	 */
114
	protected function notification_handler($step, $notification_types): string
115
	{
116
		/** @type \phpbb\notification\manager $phpbb_notifications */
117
		$phpbb_notifications = $this->container->get('notification_manager');
118
119
		foreach ($notification_types as $notification_type)
120
		{
121
			call_user_func([$phpbb_notifications, $step . '_notifications'], $notification_type);
122
		}
123
124
		return 'notifications';
125
	}
126
127
	/**
128
	 * Returns the list of notification types
129
	 *
130
	 * @return array
131
	 * @access protected
132
	 */
133
	protected function notification_types(): array
134
	{
135
		return [
136
			'skouat.ppde.notification.type.admin_donation_errors',
137
			'skouat.ppde.notification.type.admin_donation_received',
138
			'skouat.ppde.notification.type.donor_donation_received',
139
		];
140
	}
141
}
142