run()   F
last analyzed

Complexity

Conditions 37
Paths 972

Size

Total Lines 140

Duplication

Lines 23
Ratio 16.43 %

Importance

Changes 0
Metric Value
dl 23
loc 140
rs 0.0311
c 0
b 0
f 0
cc 37
nc 972
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style introduced by
File has mixed line endings; this may cause incorrect results
Loading history...
2
3
class EcommerceProductVariationTaskDeleteVariations extends BuildTask
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...
4
{
5
    protected $verbose = true;
6
7
    public static function create_link($product)
8
    {
9
        if (is_numeric($product)) {
10
            $product = Product::get()->byID($product);
11
        } elseif ($product instanceof Product) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
12
            //do nothing
13
        }
14
        if ($product) {
15
            return "dev/tasks/EcommerceProductVariationTaskDeleteVariations/?productid=".$product->ID."&live=1&silent=1";
16
        }
17
    }
18
19
    protected $title = "Deletes all the variations and associated data from a product";
20
21
    protected $description = "CAREFUL: the developer will need to supply the ID as a get variable (?productid=XXX) as well as a test / live flag (?live=1, default is test) for the product and variations will be deleted without keeping a history.";
22
23
    public function run($request)
0 ignored issues
show
Coding Style introduced by
run uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
24
    {
25
        $productVariationArrayID = array();
26
        if (empty($_GET["silent"])) {
27
            $this->verbose = true;
28
        } else {
29
            $this->verbose = intval($_GET["silent"]) == 1 ? false : true;
30
        }
31
        if (empty($_GET["productid"])) {
32
            $productID = 0;
33
        } elseif ($_GET["productid"] == 'all') {
34
            $productID = -1;
35
        } else {
36
            $productID = intval($_GET["productid"]);
37
        }
38 View Code Duplication
        if (empty($_GET["live"])) {
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...
39
            $live = false;
40
        } else {
41
            $live = intval($_GET["live"]) == 1 ? true : false;
42
        }
43 View Code Duplication
        if ($live) {
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...
44
            if ($this->verbose) {
45
                DB::alteration_message("this is a live task", "deleted");
46
            }
47
        } else {
48
            if ($this->verbose) {
49
                DB::alteration_message("this is a test only. If you add a live=1 get variable then you can make it for real ;-)", "created");
50
            }
51
        }
52
        if ($productID == -1) {
53
            $products = Product::get();
54
        } else {
55
            $products = null;
56
            $product = Product::get()->byID($productID);
57
            if ($product) {
58
                $products= new ArrayList();
59
                $products->push($product);
60
            }
61
        }
62
        if ($products && $products->count()) {
63
            foreach ($products as $product) {
64
                $productID = $product->ID;
65
                if ($products->count()) {
66
                    if ($this->verbose) {
67
                        DB::alteration_message("Deleting variations for ".$product->Title, "deleted");
68
                    }
69
                    $variations = ProductVariation::get()->filter(array("ProductID" => $productID))->limit(100);
70
                    if ($variations->count()) {
71
                        if ($this->verbose) {
72
                            DB::alteration_message("PRE DELETE COUNT: ".$variations->count());
73
                        }
74
                        foreach ($variations as $variation) {
75
                            if ($this->verbose) {
76
                                DB::alteration_message("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Deleting Variation: ".$variation->Title(), "deleted");
77
                            }
78
                            if ($live) {
79
                                $variation->delete();
80
                            }
81
                            $productVariationArrayID[$variation->ID] = $variation->ID;
82
                        }
83
                        $variations = ProductVariation::get()->filter(array("ProductID" => $productID))->limit(100);
84
                        if ($live) {
85 View Code Duplication
                            if ($variations->count()) {
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...
86
                                if ($this->verbose) {
87
                                    DB::alteration_message("POST DELETE COUNT: ".$variations->count());
88
                                }
89
                            } else {
90
                                if ($this->verbose) {
91
                                    DB::alteration_message("All variations have been deleted: ", "created");
92
                                }
93
                            }
94
                        } else {
95
                            if ($this->verbose) {
96
                                DB::alteration_message("This was a test only", "created");
97
                            }
98
                        }
99
                    } else {
100
                        if ($this->verbose) {
101
                            DB::alteration_message("There are no variations to delete", "created");
102
                        }
103
                    }
104
                    if ($this->verbose) {
105
                        DB::alteration_message("Starting cleanup", "created");
106
                    }
107
                    if ($live) {
108
                        $sql = "
109
									DELETE
110
									FROM \"Product_VariationAttributes\"
111
									WHERE \"ProductID\" = ".$productID;
112
                        if ($this->verbose) {
113
                            DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>");
114
                        }
115
                        DB::query($sql);
116
                        $sql = "
117
									DELETE \"ProductVariation_AttributeValues\"
118
									FROM \"ProductVariation_AttributeValues\"
119
										LEFT JOIN \"ProductVariation\"
120
											ON \"ProductVariation_AttributeValues\".\"ProductVariationID\" = \"ProductVariation\".\"ID\"
121
									WHERE \"ProductVariation\".\"ID\" IS NULL";
122
                        if ($this->verbose) {
123
                            DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>");
124
                        }
125
                        DB::query($sql);
126
                    } else {
127
                        $sql = "
128
									SELECT COUNT(Product_VariationAttributes.ID)
129
									FROM \"Product_VariationAttributes\"
130
									WHERE \"ProductID\" = ".$productID;
131
                        if ($this->verbose) {
132
                            DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>");
133
                        }
134
                        $result = DB::query($sql);
135
                        if ($this->verbose) {
136
                            DB::alteration_message("Would have deleted ".$result->value()." rows");
137
                        }
138
                        $sql = "
139
									SELECT COUNT (\"ProductVariation_AttributeValues\".\"ID\")
140
									FROM \"ProductVariation_AttributeValues\"
141
										LEFT JOIN \"ProductVariation\"
142
											ON \"ProductVariation_AttributeValues\".\"ProductVariationID\" = \"ProductVariation\".\"ID\"
143
									WHERE
144
										\"ProductVariation\".\"ID\" IS NULL OR
145
										\"ProductVariation\".\"ID\" IN(".implode(",", $productVariationArrayID).") ";
146
                        if ($this->verbose) {
147
                            DB::alteration_message("<pre>RUNNING<br />".$sql."</pre>");
148
                        }
149
                        $result = DB::query($sql);
150
                        if ($this->verbose) {
151
                            DB::alteration_message("Would have deleted ".$result->value()." rows");
152
                        }
153
                    }
154
                }
155
            }
156
        } else {
157
            if ($this->verbose) {
158
                DB::alteration_message("Product does not exist. You can set the product by adding it productid=XXX as a GET variable.  You can also add <i>all</i> to delete ALL product Variations.", "deleted");
159
            }
160
        }
161
        DB::alteration_message("Completed", "created");
162
    }
163
}
164
165
class EcommerceProductVariationTaskDeleteVariations_EXT extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
166
{
167
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
168
        "ecommerceproductvariationtaskdeletevariations" => true
169
    );
170
171
    //NOTE THAT updateEcommerceDevMenuConfig adds to Config options
172
    //but you can als have: updateEcommerceDevMenuDebugActions
173
    public function updateEcommerceDevMenuRegularMaintenance($buildTasks)
174
    {
175
        $buildTasks[] = "ecommerceproductvariationtaskdeletevariations";
176
        return $buildTasks;
177
    }
178
179
    public function ecommerceproductvariationtaskdeletevariations($request)
180
    {
181
        $this->owner->runTask("EcommerceProductVariationTaskDeleteVariations", $request);
182
    }
183
}
184