Completed
Push — master ( 053e09...4ba1a0 )
by Sudar
02:24
created

BaseLicense   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 203
rs 10
wmc 25
lcom 1
cbo 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A set_license_key() 0 3 1
is_valid() 0 1 ?
get_option_name() 0 1 ?
A get_license_key() 0 3 1
A set_addon_name() 0 3 1
A get_addon_name() 0 3 1
D activate() 0 46 10
A deactivate() 0 16 3
A get_version() 0 10 3
A load() 0 3 1
A store() 0 4 1
A clear() 0 5 1
1
<?php namespace EmailLog\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 $edd_api (Optional) EDD API Wrapper instance. Default is null.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $edd_api not be EDDAPI|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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 License Key.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
	 * Activate License by calling EDD API.
91
	 * The license data returned by API is stored in an option.
92
	 *
93
	 * @return object API Response JSON Object.
94
	 * @throws \Exception In case of communication errors or License Issues.
95
	 */
96
	public function activate() {
97
		$response = $this->edd_api->activate_license( $this->get_license_key(), $this->get_addon_name() );
98
99
		if ( $response->success && 'valid' === $response->license ) {
100
			$response->license_key = $this->get_license_key();
101
102
			$this->store( $response );
103
			return $response;
104
		}
105
106
		switch ( $response->error ) {
107
			case 'expired':
108
				$message = sprintf(
109
					__( 'Your license key expired on %s.' ),
110
					date_i18n( get_option( 'date_format' ), strtotime( $response->expires, current_time( 'timestamp' ) ) )
111
				);
112
				break;
113
114
			case 'revoked':
115
				$message = __( 'Your license key has been disabled.' );
116
				break;
117
118
			case 'missing':
119
				$message = __( 'Your license key is invalid.' );
120
				break;
121
122
			case 'invalid':
123
			case 'site_inactive':
124
				$message = __( 'Your license is not active for this URL.' );
125
				break;
126
127
			case 'item_name_mismatch':
128
				$message = sprintf( __( 'Your license key is not valid for %s.' ), $this->get_addon_name() );
129
				break;
130
131
			case 'no_activations_left':
132
				$message = __( 'Your license key has reached its activation limit.' );
133
				break;
134
135
			default:
136
				$message = __( 'An error occurred, please try again.' );
137
				break;
138
		}
139
140
		throw new \Exception( $message );
141
	}
142
143
	/**
144
	 * Deactivate the license by calling EDD API.
145
	 * The stored license data will be cleared.
146
	 *
147
	 * @return object API Response JSON object.
148
	 * @throws \Exception In case of communication errors.
149
	 */
150
	public function deactivate() {
151
		$response = $this->edd_api->deactivate_license( $this->get_license_key(), $this->get_addon_name() );
152
153
		if ( $response->success && 'deactivated' === $response->license ) {
154
			$this->clear();
155
			return $response;
156
		}
157
158
		switch ( $response->error ) {
159
			default:
0 ignored issues
show
Unused Code introduced by
default: $message = ...onse->error; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
160
				$message = __( 'An error occurred, please try again', 'email-log' ) . $response->error;
161
				break;
162
		}
163
164
		throw new \Exception( $message );
165
	}
166
167
	/**
168
	 * Get version information by calling EDD API.
169
	 * // TODO: Currently not used. Will be removed eventually.
170
	 *
171
	 * @return object API Response JSON Object.
172
	 * @throws \Exception In case of communication errors.
173
	 */
174
	public function get_version() {
175
		$response = $this->edd_api->get_version( $this->get_license_key(), $this->get_addon_name() );
176
177
		if ( isset( $response->new_version ) && ! isset( $response->msg ) ) {
178
			return $response;
179
		}
180
181
		$message = __( 'An error occurred, please try again', 'email-log' ) . $response->error;
182
		throw new \Exception( $message );
183
	}
184
185
	/**
186
	 * Load the license data from DB option.
187
	 */
188
	public function load() {
189
		$this->license_data = get_option( $this->get_option_name(), null );
190
	}
191
192
	/**
193
	 * Store License data in DB option.
194
	 *
195
	 * @access protected
196
	 *
197
	 * @param object $license_data License data.
198
	 */
199
	protected function store( $license_data ) {
200
		$this->license_data = $license_data;
201
		update_option( $this->get_option_name(), $license_data );
202
	}
203
204
	/**
205
	 * Clear stored license data.
206
	 *
207
	 * @access protected
208
	 */
209
	protected function clear() {
210
		$this->license_data = null;
211
		$this->license_key = null;
212
		delete_option( $this->get_option_name() );
213
	}
214
}
215