Completed
Push — master ( 49679a...617b2a )
by Sudar
02:03
created

BaseLicense::get_version()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php namespace EmailLog\License;
2
3
use EmailLog\Util\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
17
	/**
18
	 * EDD API Wrapper.
19
	 *
20
	 * @var \EmailLog\Util\EDDAPI
21
	 */
22
	protected $edd_api;
23
24
	/**
25
	 * Is the license activated and valid?
26
	 *
27
	 * @return bool True if license is active, False otherwise.
28
	 */
29
	abstract public function is_valid();
30
31
	/**
32
	 * Get the license key.
33
	 *
34
	 * @return string License Key.
35
	 */
36
	abstract public function get_license_key();
37
38
	/**
39
	 * Construct a new License object.
40
	 * If the API Wrapper is not provided, then a new one is initialized.
41
	 *
42
	 * @param \EmailLog\Util\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...
43
	 */
44
	public function __construct( $edd_api = null ) {
45
		if ( is_null( $edd_api ) ) {
46
			$edd_api = new EDDAPI();
47
		}
48
49
		$this->edd_api = $edd_api;
50
	}
51
52
	/**
53
	 * Set the license Key.
54
	 *
55
	 * @param string $license_key License Key.
56
	 */
57
	public function set_license_key( $license_key ) {
58
		$this->license_key = $license_key;
59
	}
60
61
	/**
62
	 * Set add-on name.
63
	 *
64
	 * @param string $addon_name Add-on Name.
65
	 */
66
	public function set_addon_name( $addon_name ) {
67
		$this->addon_name = $addon_name;
68
	}
69
70
	/**
71
	 * Get the add-on name.
72
	 *
73
	 * @return string Add-on name.
74
	 */
75
	public function get_addon_name() {
76
		return $this->addon_name;
77
	}
78
79
	/**
80
	 * Activate License by calling EDD API.
81
	 *
82
	 * @return object API Response JSON Object.
83
	 * @throws \Exception In case of communication errors or License Issues.
84
	 */
85
	public function activate() {
86
		$response = $this->edd_api->activate_license( $this->get_license_key(), $this->get_addon_name() );
87
88
		if ( $response->success && 'valid' === $response->license ) {
89
			return $response;
90
		}
91
92
		switch ( $response->error ) {
93
			case 'expired':
94
				$message = sprintf(
95
					__( 'Your license key expired on %s.' ),
96
					date_i18n( get_option( 'date_format' ), strtotime( $response->expires, current_time( 'timestamp' ) ) )
97
				);
98
				break;
99
100
			case 'revoked':
101
				$message = __( 'Your license key has been disabled.' );
102
				break;
103
104
			case 'missing':
105
				$message = __( 'Your license key is invalid.' );
106
				break;
107
108
			case 'invalid':
109
			case 'site_inactive':
110
				$message = __( 'Your license is not active for this URL.' );
111
				break;
112
113
			case 'item_name_mismatch':
114
				$message = sprintf( __( 'Your license key is not valid for %s.' ), $this->get_addon_name() );
115
				break;
116
117
			case 'no_activations_left':
118
				$message = __( 'Your license key has reached its activation limit.' );
119
				break;
120
121
			default:
122
				$message = __( 'An error occurred, please try again.' );
123
				break;
124
		}
125
126
		throw new \Exception( $message );
127
	}
128
129
	/**
130
	 * Deactivate the license by calling EDD API.
131
	 *
132
	 * @return object API Response JSON object.
133
	 * @throws \Exception In case of communication errors.
134
	 */
135
	public function deactivate() {
136
		$response = $this->edd_api->deactivate_license( $this->get_license_key(), $this->get_addon_name() );
137
138
		if ( $response->success && 'deactivated' === $response->license ) {
139
			return $response;
140
		}
141
142
		switch ( $response->error ) {
143
			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...
144
				$message = __( 'An error occurred, please try again', 'email-log' ) . $response->error;
145
				break;
146
		}
147
148
		throw new \Exception( $message );
149
	}
150
151
	/**
152
	 * Get version information by calling EDD API.
153
	 *
154
	 * @return object API Response JSON Object.
155
	 * @throws \Exception In case of communication errors.
156
	 */
157
	public function get_version() {
158
		$response = $this->edd_api->get_version( $this->get_license_key(), $this->get_addon_name() );
159
160
		if ( isset( $response->new_version ) && ! isset( $response->msg ) ) {
161
			return $response;
162
		}
163
164
		$message = __( 'An error occurred, please try again', 'email-log' ) . $response->error;
165
		throw new \Exception( $message );
166
	}
167
}
168