DataIntegrityTestDefaultEntries   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 5
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B update() 0 31 9
1
<?php
2
3
4
class DataIntegrityTestDefaultEntries extends Object
5
{
6
    public static function update($baseTable, $field, $value, $id = 0, $replace = false, $addLive = false)
0 ignored issues
show
Unused Code introduced by
The parameter $addLive 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...
7
    {
8
        $object = DataObject::get_one($baseTable);
9
        if ($object) {
10
            $tableArray = array($baseTable);
11
            if ($object instanceof SiteTree) {
12
                $tableArray[] = $baseTable."_Live";
13
            }
14
            foreach ($tableArray as $table) {
15
                $value = Convert::raw2sql($value);
16
                $sql = "UPDATE \"$table\" SET \"$table\".\"$field\" = '$value'";
17
                $where = array();
18
                if ($id) {
19
                    $where[] = "  \"$table\".\"ID\" = ".$id;
20
                }
21
                if (!$replace) {
22
                    $where[] = " \"$table\".\"$field\" IS NULL OR \"$table\".\"$field\" = '' OR \"$table\".\"$field\" = 0 ";
23
                }
24
                $wherePhrase = '';
25
                if (count($where)) {
26
                    $wherePhrase = " WHERE ( " . implode(") AND (", $where) . " )";
27
                }
28
                $result = DB::query("SELECT COUNT(\"$table\".\"ID\") C FROM \"$table\" ".$wherePhrase);
29
                if ($result && $result->value()) {
30
                    $sql .= $wherePhrase;
31
                    DB::query($sql);
32
                    DB::alteration_message("Updated $field in $table to $value ", "added");
33
                }
34
            }
35
        }
36
    }
37
}
38