| Conditions | 37 |
| Paths | 972 |
| Total Lines | 140 |
| Lines | 23 |
| Ratio | 16.43 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 23 | public function run($request) |
||
| 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"])) { |
|
| 39 | $live = false; |
||
| 40 | } else { |
||
| 41 | $live = intval($_GET["live"]) == 1 ? true : false; |
||
| 42 | } |
||
| 43 | View Code Duplication | if ($live) { |
|
| 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(" 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()) { |
|
| 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 | } |
||
| 184 |