Completed
Push — master ( cd526b...c8a39f )
by Christopher
60:34 queued 45:37
created

Potato::getColumnNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Ganga\Potato;
4
5
use ReflectionClass;
6
7
/**
8
 * Class that defines Potato ORM
9
 * Will be extended by Model Classes
10
 */
11
class Potato
12
{
13
    protected $tableName;
14
    protected static $id = null;
15
    protected $db;
16
    public static $table;
17
18
    /**
19
     * Constructor
20
     * TableName is set when the class is extended
21
     */
22
    public function __construct()
23
    {
24
        /**
25
         * Tests if the Model has defined a table name
26
         * if not it assigns it to the name of the class
27
         */
28
        if (!$this->tableName)
29
        {
30
            $ref = new ReflectionClass($this);
31
            $this->tableName = strtolower($ref->getShortName()).'s';
32
        }
33
34
        Self::$table = $this->tableName;
35
36
        // Test if table exists else throw an exception
37
        if (!$this->tableExists()) {
38
            throw new PotatoException("Table $this->tableName does not exist.");
39
        }
40
    }
41
42
    public function tableExists()
43
    {
44
        $query = "SELECT name FROM sqlite_master WHERE type='table' AND name='$this->tableName';";
45
        $exists = Connection::db()->querySingle($query, true);
46
47
        if (empty($exists)) {
48
            return false;
49
        } else {
50
            return true;
51
        }
52
    }
53
    /**
54
     * Get the name of the current table
55
     * @return String table name
56
     */
57
    public function getTableName()
58
    {
59
        return $this->tableName;
60
    }
61
62
    public static function getAll()
63
    {
64
        if(!Self::$table) {
65
            self::$table = strtolower(static::class) . 's';
66
        }
67
68
        $query = "SELECT * FROM ".self::$table;
69
        $results = Connection::db()->query($query);
70
        $res = [];
71
72
        while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
73
            array_push($res, $row);
74
        }
75
76
        return $res;
77
    }
78
79
    public static function getOne($id)
80
    {
81
        if(!Self::$table) {
82
            self::$table = strtolower(static::class) . 's';
83
        }
84
85
        $query = "SELECT * FROM ".self::$table." WHERE id = $id";
86
        $results = Connection::db()->query($query);
87
        $res = [];
88
89
        while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
90
            array_push($res, $row);
91
        }
92
93
        self::$id = $id;
94
95
        if (!empty($res)) {
96
            return $res;
97
        } else {
98
            throw new PotatoException('There is no user with id '. $id);
99
        }
100
    }
101
102
    public function getColumnNames()
103
    {
104
        $query = Connection::db()->query("PRAGMA table_info($this->tableName);");
105
        $columns = [];
106
107
        while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
108
            array_push($columns, $row['name']);
109
        }
110
111
        return $columns;
112
    }
113
114
    public function save()
115
    {
116
        $columnNames = $this->getColumnNames();
117
        $availableColumnNames = [];
118
        $availableColumnValues = [];
119
120
        foreach ($columnNames as $columnName)
121
        {
122
            if (isset($this->{$columnName})) {
123
                array_push($availableColumnNames, $columnName);
124
                array_push($availableColumnValues, $this->{$columnName});
125
            }
126
        }
127
128
        if (!self::$id) {
129
            // new record so we insert
130
            $query = "INSERT INTO $this->tableName (".implode(", ", $availableColumnNames).") VALUES('".implode("', '", $availableColumnValues)."');";
131
        } else {
132
            // existing record, se we update
133
            $query = "UPDATE $this->tableName SET ";
134
135
            for ($i = 0; $i < count($availableColumnNames); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
136
                $prop = $availableColumnNames[$i];
137
                $query .= " $prop = '". $this->{$prop}."'";
138
139
                if ($i != count($availableColumnNames) - 1) {
140
                    $query .= ",";
141
                }
142
            }
143
144
            $query .= " WHERE id = ".self::$id;
145
        }
146
147
        $result = Connection::db()->query($query);
148
149
        if (!$result) {
150
            return 0;
151
        } else {
152
            return 1;
153
        }
154
    }
155
156
    public static function find($id)
157
    {
158
        self::getOne($id);
159
        return new static();
160
    }
161
162
    public static function destroy($id)
163
    {
164
        if(!Self::$table) {
165
            self::$table = strtolower(static::class) . 's';
166
        }
167
168
        self::getOne($id);
169
170
        $query = "DELETE FROM ".self::$table." WHERE id = ".$id;
171
172
        $result = Connection::db()->query($query);
0 ignored issues
show
Unused Code introduced by
$result 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...
173
    }
174
}
175