Total Complexity | 49 |
Total Lines | 405 |
Duplicated Lines | 0 % |
Changes | 20 | ||
Bugs | 0 | Features | 8 |
Complex classes like Algolia_Send_Products 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 Algolia_Send_Products, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
91 | class Algolia_Send_Products |
||
92 | { |
||
93 | const PLUGIN_NAME = 'Algolia Woo Indexer'; |
||
94 | const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
||
95 | |||
96 | /** |
||
97 | * The Algolia instance |
||
98 | * |
||
99 | * @var \Algolia\AlgoliaSearch\SearchClient |
||
100 | */ |
||
101 | private static $algolia = null; |
||
102 | |||
103 | /** |
||
104 | * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
||
105 | */ |
||
106 | public static function can_connect_to_algolia() |
||
107 | { |
||
108 | try { |
||
109 | self::$algolia->listApiKeys(); |
||
110 | } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
||
111 | add_action( |
||
112 | 'admin_notices', |
||
113 | function () { |
||
114 | echo '<div class="error notice"> |
||
115 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
||
116 | </div>'; |
||
117 | } |
||
118 | ); |
||
119 | return; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * check if the field is enabled and shall be sent |
||
125 | * |
||
126 | * @param mixed $field name of field to be checked according to BASIC_FIELDS |
||
127 | * @return boolean true if enable, false is not enabled |
||
128 | */ |
||
129 | public static function is_basic_field_enabled($field) |
||
130 | { |
||
131 | $fieldValue = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
||
132 | return $fieldValue; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * helper function to add a field to a record while checking their state |
||
137 | * |
||
138 | * @param array $record existing record where the field and value shall be added to |
||
139 | * @param string $field name of field to be checked according to BASIC_FIELDS |
||
140 | * @param mixed $value data to be added to the record array named to $field |
||
141 | * @param boolean $skip_basic_field_validation set to true if it is not a basic field to skip validation |
||
142 | * @return array $record previous passed $record with added field data |
||
143 | */ |
||
144 | public static function add_to_record($record, $field, $value, $skip_basic_field_validation = false) |
||
145 | { |
||
146 | /** |
||
147 | * only if enabled or validation skipped and not empty |
||
148 | */ |
||
149 | if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { |
||
150 | return $record; |
||
151 | } |
||
152 | |||
153 | $record[$field] = $value; |
||
154 | return $record; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Get sale price or regular price based on product type |
||
159 | * |
||
160 | * @param mixed $product Product to check |
||
161 | * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price |
||
162 | */ |
||
163 | public static function get_product_type_price($product) |
||
164 | { |
||
165 | $sale_price = 0; |
||
166 | $regular_price = 0; |
||
167 | if ($product->is_type('simple')) { |
||
168 | $sale_price = $product->get_sale_price(); |
||
169 | $regular_price = $product->get_regular_price(); |
||
170 | } elseif ($product->is_type('variable')) { |
||
171 | $sale_price = $product->get_variation_sale_price('min', true); |
||
172 | $regular_price = $product->get_variation_regular_price('max', true); |
||
173 | } |
||
174 | return array( |
||
175 | 'sale_price' => $sale_price, |
||
176 | 'regular_price' => $regular_price |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Checks if stock management is enabled and if so, returns quantity and status |
||
183 | * |
||
184 | * @param mixed $product Product to check |
||
185 | * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned, |
||
186 | */ |
||
187 | public static function get_product_stock_data($product) |
||
188 | { |
||
189 | if ($product->get_manage_stock()) { |
||
190 | return array( |
||
191 | 'stock_quantity' => $product->get_stock_quantity(), |
||
192 | 'stock_status' => $product->get_stock_status() |
||
193 | ); |
||
194 | } |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get product tags |
||
200 | * |
||
201 | * @param mixed $product Product to check |
||
202 | * @return array ['tag1', 'tag2', ...] simple array with associated tags |
||
203 | */ |
||
204 | public static function get_product_tags($product) |
||
205 | { |
||
206 | $tags = get_the_terms($product->get_id(), 'product_tag'); |
||
207 | $term_array = array(); |
||
208 | if (is_array($tags)) { |
||
209 | foreach ($tags as $tag) { |
||
210 | $name = get_term($tag)->name; |
||
211 | array_push($term_array, $name); |
||
212 | } |
||
213 | } |
||
214 | return $term_array; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Get product categories |
||
219 | * |
||
220 | * @param mixed $product Product to check |
||
221 | * @return array ['tag1', 'tag2', ...] simple array with associated categories |
||
222 | */ |
||
223 | public static function get_product_categories($product) |
||
224 | { |
||
225 | $categories = get_the_terms($product->get_id(), 'product_cat'); |
||
226 | $term_array = array(); |
||
227 | foreach ($categories as $category) { |
||
228 | $name = get_term($category)->name; |
||
229 | $slug = get_term($category)->slug; |
||
230 | array_push($term_array, array( |
||
231 | "name" => $name, |
||
232 | "slug" => $slug |
||
233 | )); |
||
234 | } |
||
235 | return $term_array; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get attributes from product |
||
240 | * |
||
241 | * @param mixed $product Product to check |
||
242 | * @return array ['pa_name' => ['value1', 'value2']] Array with key set to the product attribute internal name and values as array. returns false if not attributes found. |
||
243 | */ |
||
244 | public static function get_product_attributes($product) |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Send WooCommerce products to Algolia |
||
345 | * |
||
346 | * @param Int $id Product to send to Algolia if we send only a single product |
||
347 | * @return void |
||
348 | */ |
||
349 | public static function send_products_to_algolia($id = '') |
||
496 | </div>'; |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths