Completed
Push — staging ( 78e662...0f9e2f )
by
unknown
05:16
created

Yikes_Inc_Easy_Mailchimp_API_Manager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 174
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 3

8 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
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  6.3.0
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 manager instance.
15
	 *
16
	 * @since 6.3.0
17
	 * @var Yikes_Inc_Easy_Mailchimp_API_Account
18
	 */
19
	protected $account_manager = null;
20
21
	/**
22
	 * Our API instance.
23
	 *
24
	 * @since 6.3.0
25
	 * @var Yikes_Inc_Easy_Mailchimp_API[]
26
	 */
27
	protected $api = array();
28
29
	/**
30
	 * The whole API key.
31
	 *
32
	 * @since 6.3.0
33
	 * @var string
34
	 */
35
	protected $api_key = '';
36
37
	/**
38
	 * The Datacenter for the Mailchimp account.
39
	 *
40
	 * @since 6.3.0
41
	 * @var string
42
	 */
43
	protected $dc = '';
44
45
	/**
46
	 * The API key with the datacenter portion removed.
47
	 *
48
	 * @since 6.3.0
49
	 * @var string
50
	 */
51
	protected $key = '';
52
53
	/**
54
	 * The list manager instance.
55
	 *
56
	 * @since 6.3.0
57
	 * @var Yikes_Inc_Easy_Mailchimp_API_Lists
58
	 */
59
	protected $list_manager = null;
60
61
	/**
62
	 * Yikes_Inc_Easy_Mailchimp_API_Manager constructor.
63
	 *
64
	 * @since 6.3.0
65
	 *
66
	 * @param string $api_key The full API key from Mailchimp.
67
	 */
68
	public function __construct( $api_key = '' ) {
69
		if ( empty( $api_key ) ) {
70
			$api_key = yikes_get_mc_api_key();
71
		}
72
73
		$this->api_key = $api_key;
74
		$parts         = $this->get_api_key_parts();
75
		$this->key     = $parts['key'];
76
		$this->dc      = $parts['dc'];
77
	}
78
79
	/**
80
	 * Get the API key.
81
	 *
82
	 * @author Jeremy Pry
83
	 * @since  6.3.0
84
	 * @return string The API key with the datacenter portion removed.
85
	 */
86
	public function get_api_key() {
87
		return $this->key;
88
	}
89
90
	/**
91
	 * Get the array of API Key parts.
92
	 *
93
	 * @author Jeremy Pry
94
	 * @since  6.3.0
95
	 * @return array Associative array of API key parts.
96
	 */
97
	protected function get_api_key_parts() {
98
		$parts = explode( '-', $this->api_key );
99
100
		return array(
101
			'key' => $parts[0],
102
			'dc'  => isset( $parts[1] ) ? $parts[1] : '',
103
		);
104
	}
105
106
	/**
107
	 * Get the Datacenter for the Mailchimp account.
108
	 *
109
	 * @author Jeremy Pry
110
	 * @since  6.3.0
111
	 * @return string The datacenter for the Mailchimp Account.
112
	 */
113
	public function get_datacenter() {
114
		return $this->dc;
115
	}
116
117
	/**
118
	 * Get the default version of the API to use.
119
	 *
120
	 * @author Jeremy Pry
121
	 * @since  6.3.0
122
	 * @return string
123
	 */
124
	public function get_default_api_version() {
125
		/**
126
		 * Filter the default Mailchimp API version.
127
		 *
128
		 * @since 6.3.0
129
		 *
130
		 * @param string $version The default Mailchimp API version.
131
		 */
132
		return apply_filters( 'yikesinc_eme_default_api_version', '3.0' );
133
	}
134
135
	/**
136
	 * Get the API instance.
137
	 *
138
	 * @author Jeremy Pry
139
	 * @since  6.3.0
140
	 *
141
	 * @param string $version The API version instance to retrieve.
142
	 *
143
	 * @return Yikes_Inc_Easy_Mailchimp_API
144
	 */
145
	public function get_api( $version = '' ) {
146
		$version = $version ?: $this->get_default_api_version();
147
148
		if ( ! array_key_exists( $version, $this->api ) || null === $this->api[ $version ] ) {
149
			$this->api[ $version ] = new Yikes_Inc_Easy_Mailchimp_API( $this->get_datacenter(), $this->get_api_key(), $version );
150
		}
151
152
		return $this->api[ $version ];
153
	}
154
155
	/**
156
	 * Get the List Manager instance.
157
	 *
158
	 * @author Jeremy Pry
159
	 * @since  6.3.0
160
	 * @return Yikes_Inc_Easy_Mailchimp_API_Lists
161
	 */
162
	public function get_list_handler() {
163
		if ( null == $this->list_manager ) {
164
			$this->list_manager = new Yikes_Inc_Easy_Mailchimp_API_Lists( $this->get_api() );
165
		}
166
167
		return $this->list_manager;
168
	}
169
170
	/**
171
	 * Get the Account Manager instance.
172
	 *
173
	 * @author Jeremy Pry
174
	 * @since  6.3.0
175
	 * @return Yikes_Inc_Easy_Mailchimp_API_Account
176
	 */
177
	public function get_account_handler() {
178
		if ( null === $this->account_manager ) {
179
			$this->account_manager = new Yikes_Inc_Easy_Mailchimp_API_Account( $this->get_api() );
180
		}
181
182
		return $this->account_manager;
183
	}
184
}
185