Total Complexity | 40 |
Total Lines | 201 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Measurement_Protocol_V4 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_Measurement_Protocol_V4, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class MonsterInsights_Measurement_Protocol_V4 { |
||
8 | private static $instance; |
||
9 | |||
10 | public static function get_instance() { |
||
16 | } |
||
17 | |||
18 | private $is_debug; |
||
19 | |||
20 | private $measurement_id; |
||
21 | |||
22 | private $schema; |
||
23 | |||
24 | private function __construct() { |
||
25 | $this->is_debug = monsterinsights_is_debug_mode(); |
||
26 | $this->measurement_id = monsterinsights_get_v4_id_to_output(); |
||
27 | |||
28 | $this->schema = array( |
||
29 | 'currency' => 'string', |
||
30 | 'value' => 'money', |
||
31 | 'coupon' => 'string', |
||
32 | 'transaction_id' => 'string', |
||
33 | 'affiliation' => 'string', |
||
34 | 'shipping' => 'double', |
||
35 | 'tax' => 'double', |
||
36 | 'user_id' => 'string', |
||
37 | 'items' => array( |
||
38 | 'item_id' => 'string', |
||
39 | 'item_name' => 'string', |
||
40 | 'affiliation' => 'string', |
||
41 | 'coupon' => 'string', |
||
42 | 'currency' => 'string', |
||
43 | 'discount' => 'double', |
||
44 | 'index' => 'integer', |
||
45 | 'item_brand' => 'string', |
||
46 | 'item_category' => 'string', |
||
47 | 'item_list_id' => 'string', |
||
48 | 'item_list_name' => 'string', |
||
49 | 'item_variant' => 'string', |
||
50 | 'location_id' => 'string', |
||
51 | 'price' => 'money', |
||
52 | 'quantity' => 'integer', |
||
53 | ), |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | private function get_base_url() { |
||
58 | return 'https://www.google-analytics.com/mp/collect'; |
||
59 | } |
||
60 | |||
61 | private function get_url() { |
||
62 | $api_secret = is_multisite() && is_network_admin() |
||
63 | ? MonsterInsights()->auth->get_network_measurement_protocol_secret() |
||
|
|||
64 | : MonsterInsights()->auth->get_measurement_protocol_secret(); |
||
65 | |||
66 | return add_query_arg( |
||
67 | array( |
||
68 | 'api_secret' => apply_filters('monsterinsights_get_mp_call_secret', $api_secret), |
||
69 | 'measurement_id' => $this->measurement_id, |
||
70 | ), |
||
71 | $this->get_base_url() |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | private function get_client_id( $args ) { |
||
86 | } |
||
87 | |||
88 | private function sanitize_event( $params, $schema ) { |
||
89 | $sanitized_params = array(); |
||
90 | |||
91 | foreach ( $params as $key => $value ) { |
||
92 | if ( ! array_key_exists( $key, $schema ) || |
||
93 | ( ! is_array( $value ) && gettype( $value ) === $schema[ $key ] ) |
||
94 | ) { |
||
95 | $sanitized_params[ $key ] = $value; |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | if ( is_array( $value ) && is_array( $schema[ $key ] ) ) { |
||
100 | $sanitized_params[ $key ] = array(); |
||
101 | foreach ( $value as $item_index => $item ) { |
||
102 | $sanitized_params[ $key ][ $item_index ] = $this->sanitize_event( $item, $schema[ $key ] ); |
||
103 | } |
||
104 | continue; |
||
105 | } |
||
106 | |||
107 | switch ( $schema[ $key ] ) { |
||
108 | case 'string': |
||
109 | $sanitized_params[ $key ] = (string) $value; |
||
110 | break; |
||
111 | |||
112 | case 'double': |
||
113 | $sanitized_params[ $key ] = (float) $value; |
||
114 | break; |
||
115 | |||
116 | case 'integer': |
||
117 | $sanitized_params[ $key ] = (int) $value; |
||
118 | break; |
||
119 | |||
120 | case 'money': |
||
121 | $sanitized_params[ $key ] = MonsterInsights_eCommerce_Helper::round_price( $value ); |
||
122 | break; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | return $sanitized_params; |
||
127 | } |
||
128 | |||
129 | private function validate_args( $args, $defaults ) { |
||
156 | } |
||
157 | |||
158 | private function request( $args ) { |
||
159 | if ( empty( $this->measurement_id ) ) { |
||
160 | return; |
||
161 | } |
||
162 | |||
163 | $session_id = monsterinsights_get_browser_session_id( $this->measurement_id ); |
||
164 | |||
165 | $defaults = array( |
||
166 | 'client_id' => $this->get_client_id( $args ), |
||
167 | 'events' => array(), |
||
168 | 'consent' => array( |
||
169 | 'ad_personalization' => 'GRANTED', |
||
170 | ), |
||
171 | ); |
||
172 | |||
173 | $body = $this->validate_args( $args, $defaults ); |
||
174 | |||
175 | foreach ( $body['events'] as $index => $event ) { |
||
176 | |||
177 | // Provide a default session id if not set already. |
||
178 | if ( !empty( $session_id ) && empty( $body['events'][$index]['params']['session_id'] ) ) { |
||
179 | $body['events'][$index]['params']['session_id'] = $session_id; |
||
180 | } |
||
181 | |||
182 | if ( $this->is_debug ) { |
||
183 | $body['events'][ $index ]['params']['debug_mode'] = true; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | $body = apply_filters( 'monsterinsights_mp_v4_api_call', $body ); |
||
188 | |||
189 | return wp_remote_post( |
||
190 | $this->get_url(), |
||
191 | array( |
||
192 | 'method' => 'POST', |
||
193 | 'timeout' => 5, |
||
194 | 'blocking' => $this->is_debug, |
||
195 | 'body' => wp_json_encode( $body ), |
||
196 | ) |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | public function collect( $args ) { |
||
208 | } |
||
209 | } |
||
214 |