Completed
Push — master ( 88dfbf...b0f2bc )
by
unknown
12:27
created

wps_provider_ctr   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 169
Duplicated Lines 6.51 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 11
loc 169
rs 8.3673
c 0
b 0
f 0
wmc 45
lcom 1
cbo 4

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add_meta_box() 0 6 1
B save_post() 0 20 9
B provider_products_box() 0 26 5
A get_entity_attr() 0 3 1
A get_id_attr() 0 8 2
A get_value_attr() 0 8 2
B read() 0 23 5
C create_product() 5 26 7
A read_product() 0 14 4
B update_product() 6 18 5
A delete_product() 0 5 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like wps_provider_ctr 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 wps_provider_ctr, and based on these observations, apply Extract Interface, too.

1
<?php if ( !defined( 'ABSPATH' ) ) exit;
2
/**
3
 * Fichier du controleur des metaboxes pour l'administration des clients dans wpshop / Controller file for managing metaboxes into customer administration interface
4
 *
5
 * @author Eoxia developpement team <[email protected]>
6
 * @version 1.0
7
 */
8
9
/**
10
 * Classe du controleur des fournisseurs dans wpshop / Controller class for wpshop provider
11
 *
12
 * @author Eoxia developpement team <[email protected]>
13
 * @version 1.0
14
 */
15
class wps_provider_ctr {
16
	// WORDPRESS
17
	public function __construct() {
18
		add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );
19
		add_action( 'add_meta_boxes_' . WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, array( $this, 'add_meta_box' ) );
20
	}
21
	public function add_meta_box( $third ) {
0 ignored issues
show
Unused Code introduced by
The parameter $third is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
		/*$is_provider = $this->read( $third->ID );
23
		if( isset( $is_provider ) ) {
24
			add_meta_box( 'provider_products', __( 'Provider\'s products' ), array( $this, 'provider_products_box' ), WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, 'normal', 'low', $third );
25
		}*/
26
	}
27
	public function save_post( $post_id, $post, $update ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
		if( wp_is_post_revision( $post_id ) || $post->post_type != WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS )
29
			return;
30
31
		$wps_provider_product = !empty( $_REQUEST['wps_provider_product'] ) ? (array)$_REQUEST['wps_provider_product'] : array();
32
		$is_provider = $this->read( $post_id );
33
		if( isset( $is_provider ) && !empty( $wps_provider_product ) ) {
34
			foreach( $wps_provider_product as $product_id => $product ) {
35
				switch( $product['special_provider'] ) {
36
					case 'update':
37
						$product['ID'] = $product_id;
38
						$this->update_product( $product );
39
						break;
40
					case 'delete':
41
						$this->delete_product( $product_id );
42
						break;
43
				}
44
			}
45
		}
46
	}
47
	public function provider_products_box( $third ) {
48
		global $wpdb;
49
		foreach( $this->read_product( $third->ID ) as $post ) {
50
			$attributes_display = '';
51
			$product_entity_id = wpshop_entities::get_entity_identifier_from_code( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT );
52
			$query = $wpdb->prepare( 'SELECT * FROM '. WPSHOP_DBT_ATTRIBUTE . ' WHERE entity_id = %d AND is_used_in_quick_add_form = %s AND status = %s', $product_entity_id, 'yes', 'valid' );
53
			$attributes = $wpdb->get_results( $query, ARRAY_A );
54
			if( !empty($attributes) ) {
55
				foreach( $attributes as $attribute_id => $att_def ) {
56
					$current_value = wpshop_attributes::getAttributeValueForEntityInSet( $att_def['data_type'], $att_def['id'], $product_entity_id, $post->ID );
57
					$output_specs =  array(
58
						'page_code' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
59
						'element_identifier' => $post->ID,
60
						'field_id' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT.'_'.$post->ID. '_',
61
						'current_value' => ( !empty($current_value->value) ? $current_value->value : '' )
62
					);
63
					$att = wpshop_attributes::display_attribute( $att_def['code'], 'admin', $output_specs );
64
					ob_start();
65
					require( wpshop_tools::get_template_part( WPS_ACCOUNT_DIR, WPS_ACCOUNT_PATH . WPS_ACCOUNT_DIR . '/templates/', 'backend', 'provider_products/product_provider_attribute' ) );
66
					$attributes_display .= ob_get_contents();
67
					ob_end_clean();
68
				}
69
			}
70
			require( wpshop_tools::get_template_part( WPS_ACCOUNT_DIR, WPS_ACCOUNT_PATH . WPS_ACCOUNT_DIR . '/templates/', 'backend', 'provider_products/product_provider' ) );
71
		}
72
	}
73
	// PROVIDER
74
	private $id_attr = null;
75
	private $value_attr = null;
76
	private $entity_attr = null;
77
	private function get_entity_attr() {
78
		$this->entity_attr = wpshop_entities::get_entity_identifier_from_code( WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS );
79
	}
80
	private function get_id_attr() {
81
		if( is_null( $this->id_attr ) ) {
82
			global $wpdb;
83
			$query = $wpdb->prepare( 'SELECT id FROM ' . WPSHOP_DBT_ATTRIBUTE . ' WHERE code = %s AND entity_id = %d', 'is_provider', $this->get_entity_attr() );
84
			$this->id_attr = $wpdb->get_var( $query );
85
		}
86
		return $this->id_attr;
87
	}
88
	private function get_value_attr() {
89
		if( is_null( $this->value_attr ) ) {
90
			global $wpdb;
91
			$query = $wpdb->prepare( 'SELECT id FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE attribute_id = %d AND LOWER( value ) = %s', $this->get_id_attr(), strtolower( __( 'yes', 'wpshop' ) ) );
92
			$this->value_attr = $wpdb->get_var( $query );
93
		}
94
		return $this->value_attr;
95
	}
96
	public function read( $args = array() ) {
97
		global $wpdb;
98
		if( is_array( $args ) ) {
99
			$args['post_type'] = WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS;
100
			if( isset( $args['posts_per_page'] ) ) {
101
				$args['posts_per_page'] = -1;
102
			}
103
			$query = $wpdb->prepare( 'SELECT entity_id FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER . ' WHERE attribute_id = %d AND value = %d', $this->get_id_attr(), $this->get_value_attr() );
104
			$args['post__in'] = $wpdb->get_col( $query );
105
			$query = new WP_Query( $args );
106
			$return = $query->get_posts();
107
		} elseif( is_numeric( $args ) ) {
108
			$query = $wpdb->prepare( 'SELECT value_id FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER . ' WHERE entity_id = %d AND attribute_id = %d AND value = %d', $args, $this->get_id_attr(), $this->get_value_attr() );
109
			$id = $wpdb->get_var( $query );
110
			$exist = (bool) isset( $id );
111
			if( $exist ) {
112
				$return = get_post( $args );
113
			} else {
114
				$return = null;
115
			}
116
		}
117
		return $return;
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
118
	}
119
	// CRUD PROVIDER'S PRODUCTS
120
	public function create_product( $id_provider = 0, $product ) {
121
		if( is_array( $product ) || is_object( $product ) ) {
122
			if( is_array( $product ) ) {
123
				$data_to_save = $product[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute'];
124
				unset( $product[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute'] );
125 View Code Duplication
			} elseif( is_object( $product ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
				$var = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute';
127
				$data_to_save = $product->$var;
128
				unset( $product->$var );
129
			}
130
			$product_class = new wpshop_products();
131
			$data_to_save = apply_filters( 'create_provider_product_data_to_save', array( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute' => $data_to_save, 'action' => 'autosave' ) );
0 ignored issues
show
Bug introduced by
The variable $data_to_save does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
132
			$product['post_status'] = 'pending';
133
			$product['post_type'] = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
134
			if( !isset( $product['post_title'] ) ) {
135
				$product['post_title'] = __( 'New product', 'wpshop' );
136
			}
137
			$product_id = wp_insert_post( $product );
138
			if( $id_provider != 0 ) {
139
				update_post_meta( $product_id, WPSHOP_PRODUCT_PROVIDER, array( (string) $id_provider ) );
140
			}
141
			$data_to_save['post_ID'] = $product_id;
142
			$product_class->save_product_custom_informations( $product_id, $data_to_save );
143
			return $product_id;
144
		}
145
	}
146
	public function read_product( $id_provider = 0, $args = array() ) {
147
		$args['post_type'] = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT;
148
		$args['post_status'] = array( 'pending', 'publish', 'draft', 'private' );
149
		$args['meta_key'] = WPSHOP_PRODUCT_PROVIDER;
150
		if( !isset( $args['posts_per_page'] ) ) {
151
			$args['posts_per_page'] = -1;
152
		}
153
		if( is_numeric( $id_provider ) && $id_provider != 0 ) {
154
			$args['meta_value'] = serialize( (string) $id_provider );
155
			$args['meta_compare'] = 'LIKE';
156
		}
157
		$query = new WP_Query( $args );
158
		return $query->get_posts();
159
	}
160
	public function update_product( $product ) {
161
		if( is_array( $product ) || is_object( $product ) ) {
162
			if( is_array( $product ) ) {
163
				$product_id = $product['ID'];
164
				$data_to_save = $product[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute'];
165
				unset( $product[WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute'] );
166 View Code Duplication
			} elseif( is_object( $product ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
				$product_id = $product->ID;
168
				$var = WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute';
169
				$data_to_save = $product->$var;
170
				unset( $product->$var );
171
			}
172
			$product_class = new wpshop_products();
173
			$data_to_save = apply_filters( 'update_provider_product_data_to_save', array( 'post_ID' => $product_id, 'action' => 'autosave', WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute' => $data_to_save ) );
0 ignored issues
show
Bug introduced by
The variable $product_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $data_to_save does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
174
			$product_class->save_product_custom_informations( $product_id, $data_to_save );
175
			wp_update_post( $product );
176
		}
177
	}
178
	public function delete_product( $args ) {
179
		if( is_numeric( $args ) || is_object( $args ) ) {
180
			wp_trash_post( $args );
181
		}
182
	}
183
}
184