Completed
Push — master ( be4f05...f2016e )
by Sudar
01:42
created

BaseLicense::activate()   C

Complexity

Conditions 10
Paths 9

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 9
nop 0
dl 0
loc 47
ccs 0
cts 36
cp 0
crap 110
rs 5.1578
c 0
b 0
f 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 namespace EmailLog\Addon\License;
2
3
use EmailLog\Addon\API\EDDAPI;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Base class for for Bundle License and Add-on License.
9
 *
10
 * @since 2.0.0
11
 */
12
abstract class BaseLicense {
13
14
	protected $addon_name;
15
	protected $license_key;
16
	protected $license_data;
17
18
	/**
19
	 * EDD API Wrapper.
20
	 *
21
	 * @var \EmailLog\Addon\API\EDDAPI
22
	 */
23
	protected $edd_api;
24
25
	/**
26
	 * Is the license activated and valid?
27
	 *
28
	 * @return bool True if license is active, False otherwise.
29
	 */
30
	abstract public function is_valid();
31
32
	/**
33
	 * Get the option name in which license data will be stored.
34
	 *
35
	 * @return string Option name.
36
	 */
37
	abstract protected function get_option_name();
38
39
	/**
40
	 * Construct a new License object.
41
	 * If the API Wrapper is not provided, then a new one is initialized.
42
	 *
43
	 * @param \EmailLog\Addon\API\EDDAPI|null $edd_api (Optional) EDD API Wrapper instance. Default is null.
44
	 */
45
	public function __construct( $edd_api = null ) {
46
		if ( is_null( $edd_api ) ) {
47
			$edd_api = new EDDAPI();
48
		}
49
50
		$this->edd_api = $edd_api;
51
	}
52
53
	/**
54
	 * Set the license Key.
55
	 *
56
	 * @param string $license_key License Key.
57
	 */
58
	public function set_license_key( $license_key ) {
59
		$this->license_key = $license_key;
60
	}
61
62
	/**
63
	 * Get the license key.
64
	 *
65
	 * @return string|null License Key.
66
	 */
67
	public function get_license_key() {
68
		return $this->license_key;
69
	}
70
71
	/**
72
	 * Set add-on name.
73
	 *
74
	 * @param string $addon_name Add-on Name.
75
	 */
76
	public function set_addon_name( $addon_name ) {
77
		$this->addon_name = $addon_name;
78
	}
79
80
	/**
81
	 * Get the add-on name.
82
	 *
83
	 * @return string Add-on name.
84
	 */
85
	public function get_addon_name() {
86
		return $this->addon_name;
87
	}
88
89
	/**
90
	 * Get the expiry date of the license.
91
	 *
92
	 * @return false|string Expiry date in `yyyy-mm-dd hh:mm:ss` format if license is valid, False otherwise.
93
	 */
94
	public function get_expiry_date() {
95
		if ( ! empty( $this->license_data ) && isset( $this->license_data->expires ) ) {
96
			return $this->license_data->expires;
97
		}
98
99
		return false;
100
	}
101
102
	/**
103
	 * Activate License by calling EDD API.
104
	 * The license data returned by API is stored in an option.
105
	 *
106
	 * @throws \Exception In case of communication errors or License Issues.
107
	 *
108
	 * @return object API Response JSON Object.
109
	 */
110
	public function activate() {
111
		$response = $this->edd_api->activate_license( $this->get_license_key(), $this->get_addon_name() );
112
113
		if ( $response->success && 'valid' === $response->license ) {
114
			$response->license_key = $this->get_license_key();
115
116
			$this->store( $response );
117
118
			return $response;
119
		}
120
121
		switch ( $response->error ) {
122
			case 'expired':
123
				$message = sprintf(
124
					__( 'Your license key expired on %s.' , 'email-log'),
125
					date_i18n( get_option( 'date_format' ), strtotime( $response->expires, current_time( 'timestamp' ) ) )
126
				);
127
				break;
128
129
			case 'revoked':
130
				$message = __( 'Your license key has been disabled.' , 'email-log');
131
				break;
132
133
			case 'missing':
134
				$message = __( 'Your license key is invalid.' , 'email-log');
135
				break;
136
137
			case 'invalid':
138
			case 'site_inactive':
139
				$message = __( 'Your license is not active for this URL.' , 'email-log');
140
				break;
141
142
			case 'item_name_mismatch':
143
				$message = sprintf( __( 'Your license key is not valid for %s.' , 'email-log'), $this->get_addon_name() );
144
				break;
145
146
			case 'no_activations_left':
147
				$message = __( 'Your license key has reached its activation limit.' , 'email-log');
148
				break;
149
150
			default:
151
				$message = __( 'An error occurred, please try again.' , 'email-log');
152
				break;
153
		}
154
155
		throw new \Exception( $message );
156
	}
157
158
	/**
159
	 * Deactivate the license by calling EDD API.
160
	 * The stored license data will be cleared.
161
	 *
162
	 * @throws \Exception In case of communication errors.
163
	 *
164
	 * @return object API Response JSON object.
165
	 */
166
	public function deactivate() {
167
		$response = $this->edd_api->deactivate_license( $this->get_license_key(), $this->get_addon_name() );
168
169
		if ( $response->success && 'deactivated' === $response->license ) {
170
			$this->clear();
171
172
			return $response;
173
		}
174
175
		$message = __( 'An error occurred, please try again.', 'email-log' );
176
177
		if ( isset( $response->error ) ) {
178
			$message .= ' ' . $response->error;
179
		}
180
181
		throw new \Exception( $message );
182
	}
183
184
	/**
185
	 * Get version information by calling EDD API.
186
	 * // TODO: Currently not used. Will be removed eventually.
187
	 *
188
	 * @throws \Exception In case of communication errors.
189
	 *
190
	 * @return object API Response JSON Object.
191
	 */
192
	public function get_version() {
193
		$response = $this->edd_api->get_version( $this->get_license_key(), $this->get_addon_name() );
194
195
		if ( isset( $response->new_version ) && ! isset( $response->msg ) ) {
196
			return $response;
197
		}
198
199
		$message = __( 'An error occurred, please try again', 'email-log' ) . $response->error;
200
201
		throw new \Exception( $message );
202
	}
203
204
	/**
205
	 * Load the license data from DB option.
206
	 */
207
	public function load() {
208
		$this->license_data = get_option( $this->get_option_name(), null );
209
	}
210
211
	/**
212
	 * Store License data in DB option.
213
	 *
214
	 * @access protected
215
	 *
216
	 * @param object $license_data License data.
217
	 */
218
	protected function store( $license_data ) {
219
		$this->license_data = $license_data;
220
		update_option( $this->get_option_name(), $license_data );
221
	}
222
223
	/**
224
	 * Clear stored license data.
225
	 *
226
	 * @access protected
227
	 */
228
	protected function clear() {
229
		$this->license_data = null;
230
		$this->license_key  = null;
231
		delete_option( $this->get_option_name() );
232
	}
233
}
234