CountryPricesCleanup   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 39
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 32 7
1
<?php
2
3
4
class CountryPricesCleanup extends BuildTask
5
{
6
    protected $title = "Delete all Country Prices without an object";
7
8
    protected $description = "Go through each Country Price and delete it if the relating (parent) object does not exist or the price is zero OR the currency is obsolete.";
9
10
    public function run($request)
11
    {
12
        echo "<h1>Total count: ".CountryPrice::get()->count()."</h1>";
13
        increase_time_limit_to(3600);
14
        increase_memory_limit_to('512M');
15
        for ($i = 0; $i < 1000000; $i = $i + 100) {
16
            $objects = CountryPrice::get()->limit(100, $i);
17
            if ($objects->count() == 0) {
18
                echo "<h1>Total count in the end: ".CountryPrice::get()->count()."</h1>";
19
                die("=========================== THE END =============================");
20
            }
21
            flush();
22
            ob_end_flush();
23
            DB::alteration_message("<h2>Limit $i, 100</h2>");
24
            ob_start();
25
            foreach ($objects as $object) {
26
                if ($object->Price == 0 || !$object->Buyable() || $object->isObsolete()) {
27
                    flush();
28
                    ob_end_flush();
29
                    DB::alteration_message("Deleting object: ".$object->ID, "deleted");
30
                    ob_start();
31
                    $object->delete();
32
                    DB::query("DELETE FROM CountryPrice WHERE ID = ".$object->ID);
33
                } else {
34
                    flush();
35
                    ob_end_flush();
36
                    DB::alteration_message("object OK: ".$object->ID, "created");
37
                    ob_start();
38
                }
39
            }
40
        }
41
    }
42
}
43