DataIntegrityTestRecentlyChanged   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 90
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C run() 0 62 13
A minutesToTime() 0 7 1
1
<?php
2
3
4
class DataIntegrityTestRecentlyChanged extends BuildTask
5
{
6
7
8
    /**
9
     * standard SS variable
10
     * @var String
11
     */
12
    protected $title = "Check what records have been changed in the last xxx minutes";
13
14
    /**
15
     * standard SS variable
16
     * @var String
17
     */
18
    protected $description = "Go through all tables in the database and see what records have been edited in the last xxx minutes.  You can set the minutes using a GET variable (http://www.sunnysideup.co.nz/dev/tasks/DataIntegrityTestRecentlyChanged/?x=123 where 123 is the number of minutes).";
19
20
    /**
21
     * runs the task and outputs directly to the screen
22
     */
23
    public function run($request)
24
    {
25
        echo "<style>table {width: 100%;} th, td {padding: 5px; font-size: 12px; border: 1px solid #ccc; vertical-align: top;}</style>";
26
        $minutes = intval($request->getVar("m"))-0;
27
        if ($request->getVar("m") === $minutes) {
28
            //do nothing
29
        } else {
30
            $tsFrom = strtotime($request->getVar("m"));
31
            if ($tsFrom) {
32
                $tsUntil = strtotime("NOW");
33
                $minutes = round(($tsUntil - $tsFrom) / 60);
34
            }
35
        }
36
        if ($minutes) {
37
            $ts = strtotime($minutes." minutes ago");
38
            $date =  date(DATE_RFC2822, $ts);
39
            echo "<hr /><h3>changes in the last ".$this->minutesToTime($minutes)."<br />from: ".$date."<br />make sure you see THE END at the bottom of this list</h3><hr />";
40
            $whereStatementFixed = "UNIX_TIMESTAMP(\"LastEdited\") > ".$ts." ";
41
            $dataClasses = ClassInfo::subclassesFor('DataObject');
42
            array_shift($dataClasses);
43
            foreach ($dataClasses as $dataClass) {
44
                if (class_exists($dataClass)) {
45
                    $singleton = Injector::inst()->get($dataClass);
46
                    if ($singleton instanceof TestOnly) {
47
                        //do nothing
48
                    } else {
49
                        $whereStatement = $whereStatementFixed." AND \"ClassName\" = '".$dataClass."' ";
50
                        $count = $dataClass::get()->where($whereStatement)->count();
51
                        $fields = Config::inst()->get($dataClass, "db", Config::INHERITED);
52
                        if (!is_array($fields)) {
53
                            $fields = array();
54
                        }
55
                        $fields = array("ID" => "Int", "Created" => "SS_DateAndTime", "LastEdited" => "SS_DateAndTime")+$fields;
56
                        if ($count) {
57
                            echo "<h2>".$singleton->singular_name()."(".$count.")</h2>";
58
                            $objects = $dataClass::get()->where($whereStatement)->limit(1000);
59
                            foreach ($objects as $object) {
60
                                echo "<h4>".$object->getTitle()."</h4><ul>";
61
                                if (count($fields)) {
62
                                    $array = array();
0 ignored issues
show
Unused Code introduced by
$array is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
                                    foreach ($fields as $field => $typeOfField) {
64
                                        echo "<li><strong>".$field."</strong><pre>\t\t".htmlentities($object->$field)."</pre></li>";
65
                                    }
66
                                }
67
                                echo "</ul>";
68
                            }
69
                            echo "</blockquote>";
70
                        }
71
                    }
72
                }
73
            }
74
            echo "<hr /><h1>-------- THE END --------</h1>";
75
        }
76
        if (empty($_GET["m"])) {
77
            $_GET["m"] = 0;
78
        }
79
        echo "
80
			<form method=\"get\" action=\"".Director::absoluteURL("dev/tasks/".$this->class."/")."\">
81
				<label for=\"m\">please enter minutes ago or any date (e.g. last week, yesterday, 2011-11-11, etc...)</label>
82
				<input name=\"m\" id=\"m\" value=\"".$_GET["m"]."\">
83
			</form>";
84
    }
85
86
    protected function minutesToTime($minutes)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
87
    {
88
        $seconds = $minutes * 60;
89
        $dtF = new DateTime("@0");
90
        $dtT = new DateTime("@$seconds");
91
        return $dtF->diff($dtT)->format('%a days, %h hours,  and %i minutes');
92
    }
93
}
94