vindi/vindi-woocommerce

View on GitHub
src/controllers/ProductController.php

Summary

Maintainability
B
4 hrs
Test Coverage
<?php
 
namespace VindiPaymentGateways;
 
/**
* Creation and edition of products with reflection within Vindi
*
* Warning, by default, this class does not return any status.
*
* @since 1.0.0
*
*/
 
class ProductController
{
 
/**
* @var array
*/
Avoid unused private fields such as '$types'.
private $types;
 
/**
* @var VindiRoutes
*/
private $routes;
 
/**
* @var VindiLogger
*/
private $logger;
 
/**
* @var array
*/
private $ignoredTypes;
 
Visibility must be declared on method "__construct"
Line indented incorrectly; expected 4 spaces, found 2
function __construct(VindiSettings $vindi_settings)
Line indented incorrectly; expected at least 4 spaces, found 2
{
$this->routes = $vindi_settings->routes;
$this->logger = $vindi_settings->logger;
 
/**
* Define wich product types to NOT handle in this controller.
* Basically they are the same as the PlansController, but
* the check is reversed to ignore this types
*/
$this->ignoredTypes = array('variable-subscription', 'subscription');
 
add_action('wp_insert_post', array($this, 'create'), 10, 3);
add_action('wp_trash_post', array($this, 'trash'), 10, 1);
add_action('untrash_post', array($this, 'untrash'), 10, 1);
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* When the user creates a product in Woocomerce, it is created in the Vindi.
*
* @since 1.2.2
* @version 1.2.0
*
* @SuppressWarnings(PHPMD.MissingImport)
*/
Method `create` has 35 lines of code (exceeds 25 allowed). Consider refactoring.
Function `create` has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
The method create() has an NPath complexity of 768. The configured NPath complexity threshold is 200.
The method create() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10.
The method create has a boolean flag argument $recreated, which is a certain sign of a Single Responsibility Principle violation.
Avoid unused parameters such as '$update'.
Avoid unused parameters such as '$post'.
Visibility must be declared on method "create"
Line indented incorrectly; expected 4 spaces, found 2
function create($post_id, $post, $update, $recreated = false)
Line indented incorrectly; expected at least 4 spaces, found 2
{
// Check if the post is a draft
if (strpos(get_post_status($post_id), 'draft') !== false) {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
// Check if the post is product
if (get_post_type($post_id) != 'product') {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
$post_meta = new PostMeta();
if ($post_meta->check_vindi_item_id($post_id, 'vindi_product_id') > 1) {
update_post_meta($post_id, 'vindi_product_id', '');
}
 
// Check if it's a new post
// The $update value is unreliable because of the auto_draft functionality
Line exceeds 120 characters; contains 128 characters
Expected 1 space after IF keyword; 0 found
if(!$recreated && get_post_status($post_id) != 'publish' || !empty(get_post_meta($post_id, 'vindi_product_id', true))) {
Line indented incorrectly; expected at least 12 spaces, found 10
return $this->update($post_id);
}
 
$product = wc_get_product($post_id);
 
// Check if the post is NOT of the subscription type
if (in_array($product->get_type(), $this->ignoredTypes)) {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
$data = $product->get_data();
 
// Creates the product within the Vindi
$createdProduct = $this->routes->createProduct(array(
'name' => VINDI_PREFIX_PRODUCT . $data['name'],
'code' => 'WC-' . $data['id'],
'status' => ($data['status'] == 'publish') ? 'active' : 'inactive',
'invoice' => 'always',
'pricing_schema' => array(
'price' => ($data['price']) ? $data['price'] : 0,
'schema_type' => 'flat',
)
));
 
// Saving product id and plan in the WC goal
if ($createdProduct && isset($createdProduct['id'])) {
Expected 0 spaces before closing bracket; 1 found
Space after opening parenthesis of function call prohibited
Line indented incorrectly; expected at least 14 spaces, found 12
update_post_meta( $post_id, 'vindi_product_id', $createdProduct['id'] );
Line indented incorrectly; expected at least 14 spaces, found 12
set_transient('vindi_product_message', 'created', 60);
The method create uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Line indented incorrectly; expected 12 spaces, found 10
} else {
Line indented incorrectly; expected at least 16 spaces, found 12
set_transient('vindi_product_message', 'error', 60);
Line indented incorrectly; expected 12 spaces, found 10
}
 
Avoid too many `return` statements within this method.
Line indented incorrectly; expected at least 12 spaces, found 8
return $createdProduct;
Line indented incorrectly; expected 4 spaces, found 2
}
 
Method `update` has 28 lines of code (exceeds 25 allowed). Consider refactoring.
Visibility must be declared on method "update"
Line indented incorrectly; expected 4 spaces, found 2
function update($post_id)
Line indented incorrectly; expected at least 4 spaces, found 2
{
$product = wc_get_product($post_id);
 
// Check if the post is NOT of the subscription type
if (in_array($product->get_type(), $this->ignoredTypes)) {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
// Checks whether there is a vindi product ID associated within
$vindi_product_id = get_post_meta($post_id, 'vindi_product_id', true);
 
Blank line found at start of control structure
Expected 1 space after IF keyword; 0 found
if(empty($vindi_product_id)) {
 
Line indented incorrectly; expected at least 12 spaces, found 10
return $this->create($post_id, '', '', true);
}
 
$data = $product->get_data();
 
// Updates the product within the Vindi
$updatedProduct = $this->routes->updateProduct(
Multi-line function call not indented correctly; expected 12 spaces but found 10
$vindi_product_id,
Multi-line function call not indented correctly; expected 12 spaces but found 10
array(
'name' => VINDI_PREFIX_PRODUCT . $data['name'],
'code' => 'WC-' . $data['id'],
'status' => ($data['status'] == 'publish') ? 'active' : 'inactive',
'invoice' => 'always',
'pricing_schema' => array(
'price' => ($data['price']) ? $data['price'] : 0,
'schema_type' => 'flat',
)
Multi-line function call not indented correctly; expected 12 spaces but found 10
)
Line indented incorrectly; expected at least 10 spaces, found 8
);
 
Expected 1 space after IF keyword; 0 found
Line indented incorrectly; expected 10 spaces, found 8
if($updatedProduct) {
Line indented incorrectly; expected at least 14 spaces, found 10
set_transient('vindi_product_message', 'updated', 60);
The method update uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
Line indented incorrectly; expected 12 spaces, found 8
} else {
Line indented incorrectly; expected at least 16 spaces, found 10
set_transient('vindi_product_message', 'error', 60);
Line indented incorrectly; expected 12 spaces, found 8
}
 
Line indented incorrectly; expected at least 12 spaces, found 8
return $updatedProduct;
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* When the user trashes a product in Woocomerce, it is deactivated in the Vindi.
*
* @since 1.0.1
* @version 1.0.1
*/
Visibility must be declared on method "trash"
Line indented incorrectly; expected 4 spaces, found 2
function trash($post_id)
Line indented incorrectly; expected at least 4 spaces, found 2
{
// Check if the post is product
if (get_post_type($post_id) != 'product') {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
$product = wc_get_product($post_id);
 
// Check if the post is NOT of the subscription type
if (in_array($product->get_type(), $this->ignoredTypes)) {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
$vindi_product_id = get_post_meta($post_id, 'vindi_product_id', true);
 
Expected 1 space after IF keyword; 0 found
if(empty($vindi_product_id)) {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
// Changes the product status within the Vindi
$inactivatedProduct = $this->routes->updateProduct($vindi_product_id, array(
'status' => 'inactive',
));
 
Line indented incorrectly; expected at least 10 spaces, found 8
return $inactivatedProduct;
Line indented incorrectly; expected 4 spaces, found 2
}
 
/**
* When the user untrashes a product in Woocomerce, it is activated in the Vindi.
*
* @since 1.0.01
* @version 1.0.0
*/
Visibility must be declared on method "untrash"
Line indented incorrectly; expected 4 spaces, found 2
function untrash($post_id)
Line indented incorrectly; expected at least 4 spaces, found 2
{
// Check if the post is product
if (get_post_type($post_id) != 'product') {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
$product = wc_get_product($post_id);
 
// Check if the post is NOT of the subscription type
if (in_array($product->get_type(), $this->ignoredTypes)) {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
$vindi_product_id = get_post_meta($post_id, 'vindi_product_id', true);
 
Expected 1 space after IF keyword; 0 found
if(empty($vindi_product_id)) {
Line indented incorrectly; expected at least 12 spaces, found 10
return;
}
 
// Changes the product status within the Vindi
$activatedProduct = $this->routes->updateProduct($vindi_product_id, array(
'status' => 'active',
));
 
Line indented incorrectly; expected at least 10 spaces, found 8
return $activatedProduct;
Line indented incorrectly; expected 4 spaces, found 2
}
}