Completed
Push — staging ( 68fed3...485a71 )
by Evan
04:30
created

Yikes_Inc_Easy_MailChimp_API_Manager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 245
rs 10
c 0
b 0
f 0
wmc 21
lcom 2
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A get_api_key() 0 3 1
A get_api_key_parts() 0 8 2
A get_datacenter() 0 3 1
A get_default_api_version() 0 10 1
A get_api() 0 9 4
A get_list_handler() 0 7 2
A get_account_handler() 0 7 2
A get_chimp_chatter() 0 7 2
A get_profile_handler() 0 7 2
A get_account_details_handler() 0 7 2
1
<?php
2
3
/**
4
 * The MailChimp API manager.
5
 *
6
 * Used to retrieve API functionality for use elsewhere.
7
 *
8
 * @author Jeremy Pry
9
 * @since  %VERSION%
10
 */
11
class Yikes_Inc_Easy_MailChimp_API_Manager {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
13
	/**
14
	 * The account details instance.
15
	 *
16
	 * @since %VERSION%
17
	 * @var Yikes_Inc_Easy_MailChimp_API_Account_Details
18
	 */
19
	protected $account_details = null;
20
21
	/**
22
	 * The account manager instance.
23
	 *
24
	 * @since %VERSION%
25
	 * @var Yikes_Inc_Easy_MailChimp_API_Account
26
	 */
27
	protected $account_manager = null;
28
29
	/**
30
	 * Our API instance.
31
	 *
32
	 * @since %VERSION%
33
	 * @var Yikes_Inc_Easy_MailChimp_API[]
34
	 */
35
	protected $api = array();
36
37
	/**
38
	 * The whole API key.
39
	 *
40
	 * @since %VERSION%
41
	 * @var string
42
	 */
43
	protected $api_key = '';
44
45
	/**
46
	 * Yikes_Inc_Easy_MailChimp_API_Chimp_Chatter instance.
47
	 *
48
	 * @since %VERSION%
49
	 * @var Yikes_Inc_Easy_MailChimp_API_Chimp_Chatter
50
	 */
51
	protected $chimp_chatter = null;
52
53
	/**
54
	 * The Datacenter for the MailChimp account.
55
	 *
56
	 * @since %VERSION%
57
	 * @var string
58
	 */
59
	protected $dc = '';
60
61
	/**
62
	 * The API key with the datacenter portion removed.
63
	 *
64
	 * @since %VERSION%
65
	 * @var string
66
	 */
67
	protected $key = '';
68
69
	/**
70
	 * The list manager instance.
71
	 *
72
	 * @since %VERSION%
73
	 * @var Yikes_Inc_Easy_MailChimp_API_Lists
74
	 */
75
	protected $list_manager = null;
76
77
	/**
78
	 * Yikes_Inc_Easy_MailChimp_API_Profile instance.
79
	 *
80
	 * @since %VERSION%
81
	 * @var Yikes_Inc_Easy_MailChimp_API_Profile
82
	 */
83
	protected $profile = null;
84
85
	/**
86
	 * Yikes_Inc_Easy_MailChimp_API_Manager constructor.
87
	 *
88
	 * @since %VERSION%
89
	 *
90
	 * @param string $api_key The full API key from MailChimp.
91
	 */
92
	public function __construct( $api_key = '' ) {
93
		if ( empty( $api_key ) ) {
94
			$api_key = yikes_get_mc_api_key();
95
		}
96
97
		$this->api_key = $api_key;
98
		$parts         = $this->get_api_key_parts();
99
		$this->key     = $parts['key'];
100
		$this->dc      = $parts['dc'];
101
	}
102
103
	/**
104
	 * Get the API key.
105
	 *
106
	 * @author Jeremy Pry
107
	 * @since  %VERSION%
108
	 * @return string The API key with the datacenter portion removed.
109
	 */
110
	public function get_api_key() {
111
		return $this->key;
112
	}
113
114
	/**
115
	 * Get the array of API Key parts.
116
	 *
117
	 * @author Jeremy Pry
118
	 * @since  %VERSION%
119
	 * @return array Associative array of API key parts.
120
	 */
121
	protected function get_api_key_parts() {
122
		$parts = explode( '-', $this->api_key );
123
124
		return array(
125
			'key' => $parts[0],
126
			'dc'  => isset( $parts[1] ) ? $parts[1] : '',
127
		);
128
	}
129
130
	/**
131
	 * Get the Datacenter for the MailChimp account.
132
	 *
133
	 * @author Jeremy Pry
134
	 * @since  %VERSION%
135
	 * @return string The datacenter for the MailChimp Account.
136
	 */
137
	public function get_datacenter() {
138
		return $this->dc;
139
	}
140
141
	/**
142
	 * Get the default version of the API to use.
143
	 *
144
	 * @author Jeremy Pry
145
	 * @since  %VERSION%
146
	 * @return string
147
	 */
148
	public function get_default_api_version() {
149
		/**
150
		 * Filter the default MailChimp API version.
151
		 *
152
		 * @since %VERSION%
153
		 *
154
		 * @param string $version The default MailChimp API version.
155
		 */
156
		return apply_filters( 'yikesinc_eme_default_api_version', '3.0' );
157
	}
158
159
	/**
160
	 * Get the API instance.
161
	 *
162
	 * @author Jeremy Pry
163
	 * @since  %VERSION%
164
	 *
165
	 * @param string $version The API version instance to retrieve.
166
	 *
167
	 * @return Yikes_Inc_Easy_MailChimp_API
168
	 */
169
	public function get_api( $version = '' ) {
170
		$version = $version ?: $this->get_default_api_version();
171
172
		if ( ! array_key_exists( $version, $this->api ) || null === $this->api[ $version ] ) {
173
			$this->api[ $version ] = new Yikes_Inc_Easy_MailChimp_API( $this->get_datacenter(), $this->get_api_key(), $version );
174
		}
175
176
		return $this->api[ $version ];
177
	}
178
179
	/**
180
	 * Get the List Manager instance.
181
	 *
182
	 * @author Jeremy Pry
183
	 * @since  %VERSION%
184
	 * @return Yikes_Inc_Easy_MailChimp_API_Lists
185
	 */
186
	public function get_list_handler() {
187
		if ( null == $this->list_manager ) {
188
			$this->list_manager = new Yikes_Inc_Easy_MailChimp_API_Lists( $this->get_api() );
189
		}
190
191
		return $this->list_manager;
192
	}
193
194
	/**
195
	 * Get the Account Manager instance.
196
	 *
197
	 * @author Jeremy Pry
198
	 * @since  %VERSION%
199
	 * @return Yikes_Inc_Easy_MailChimp_API_Account
200
	 */
201
	public function get_account_handler() {
202
		if ( null === $this->account_manager ) {
203
			$this->account_manager = new Yikes_Inc_Easy_MailChimp_API_Account( $this->get_api() );
204
		}
205
206
		return $this->account_manager;
207
	}
208
209
	/**
210
	 * Get the chimp chatter instance.
211
	 *
212
	 * @author Jeremy Pry
213
	 * @since  %VERSION%
214
	 * @return Yikes_Inc_Easy_MailChimp_API_Chimp_Chatter
215
	 */
216
	public function get_chimp_chatter() {
217
		if ( null === $this->chimp_chatter ) {
218
			$this->chimp_chatter = new Yikes_Inc_Easy_MailChimp_API_Chimp_Chatter( $this->get_api( '2.0' ) );
0 ignored issues
show
Deprecated Code introduced by
The class Yikes_Inc_Easy_MailChimp_API_Chimp_Chatter has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
219
		}
220
221
		return $this->chimp_chatter;
222
	}
223
224
	/**
225
	 * Get the User Profile instance.
226
	 *
227
	 * This uses the V2 API.
228
	 *
229
	 * @author Jeremy Pry
230
	 * @since  %VERSION%
231
	 * @return Yikes_Inc_Easy_MailChimp_API_Profile
232
	 */
233
	public function get_profile_handler() {
234
		if ( null === $this->profile ) {
235
			$this->profile = new Yikes_Inc_Easy_MailChimp_API_Profile( $this->get_api( '2.0' ) );
0 ignored issues
show
Deprecated Code introduced by
The class Yikes_Inc_Easy_MailChimp_API_Profile has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
236
		}
237
238
		return $this->profile;
239
	}
240
241
	/**
242
	 * Get the Account Details instance.
243
	 *
244
	 * @author Jeremy Pry
245
	 * @since  %VERSION%
246
	 * @return Yikes_Inc_Easy_MailChimp_API_Account_Details
247
	 */
248
	public function get_account_details_handler() {
249
		if ( null === $this->account_details ) {
250
			$this->account_details = new Yikes_Inc_Easy_MailChimp_API_Account_Details( $this->get_api( '2.0' ) );
0 ignored issues
show
Deprecated Code introduced by
The class Yikes_Inc_Easy_MailChimp_API_Account_Details has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
251
		}
252
253
		return $this->account_details;
254
	}
255
}
256