Completed
Push — master ( 762c1d...1cb9f7 )
by Adeola
02:35
created

DataBaseModel   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 35.82%

Importance

Changes 8
Bugs 1 Features 2
Metric Value
wmc 26
c 8
b 1
f 2
lcom 1
cbo 8
dl 0
loc 206
ccs 24
cts 67
cp 0.3582
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 4 1
A __get() 0 4 1
A __construct() 0 9 2
A getAll() 0 10 2
B save() 0 24 5
A findById() 0 15 4
A getById() 0 8 2
A destroy() 0 14 4
A getClassName() 0 6 1
A checkIfRecordIsEmpty() 0 8 2
A checkIfRecordExist() 0 7 2
1
<?php
2
3
/**
4
 * Class DataBaseModel:
5
 * This is an abstract class which stands as a model
6
 * to another class e.g User class which can inherit from
7
 * all its methods. This class stands as a middle man
8
 * between the User class and the DataBaseQuery class.
9
 *
10
 * @author: Raimi Ademola <[email protected]>
11
 * @copyright: 2016 Andela
12
 */
13
namespace Demo;
14
15
use Doctrine\Common\Inflector\Inflector;
16
17
abstract class DataBaseModel implements DataBaseModelInterface
18
{
19
    protected $tableName;
20
    protected $dataBaseConnection;
21
    protected $DataBaseQuery;
22
    protected $properties;
23
    protected $arrayField = [];
24
25
    /**
26
     * This is a constructor; a default method  that will be called automatically during class instantiation.
27
     */
28 21
    public function __construct($dbConn = null)
29
    {
30 21
        if (is_null($dbConn)) {
31
            $dbConn = $this->dataBaseConnection;
32
        }    
33 21
        $this->tableName = self::getClassName();
34 21
        $this->DataBaseQuery = new DataBaseQuery($dbConn);
35 21
        $this->arrayField['id'] = 0;
36 21
    }
37
38
    /**
39
     * The magic setter method.
40
     *
41
     * @param $properties
42
     * @param $values
43
     *
44
     * @return array associative array properties
45
     */
46
    public function __set($properties, $values)
47
    {
48
        $this->arrayField[$properties] = $values;
49
    }
50
51
    /**
52
     * The magic getter method.
53
     *
54
     * @param $properties
55
     *
56
     * @return array key
57
     */
58
    public function __get($properties)
59
    {
60
        return $this->arrayField[$properties];
61
    }
62
63
    /**
64
     * This method gets all the record from a particular table
65
     * by accessing the read method from the DataBaseQuery class.
66
     *
67
     * @throws NoDataFoundException
68
     *
69
     * @return associative array
70
     */
71 6
    public static function getAll($dbConn = null)
72
    {
73 6
        $sqlData = DataBaseQuery::read($id = false, self::getClassName(), $dbConn);
74
75 3
        if (count($sqlData) > 0) {
76 3
            return $sqlData;
77
        }
78
79
        throw new NoDataFoundException('There is no data to display');
80
    }
81
82
    /**
83
     * This method either create or update record in a database table
84
     * by calling either the read method or create method in the
85
     * DataBaseQuery class.
86
     *
87
     * @throws NoRecordUpdateException
88
     * @throws EmptyArrayException
89
     * @throws NoRecordCreatedException
90
     *
91
     * @return bool true or false;
92
     */
93
    public function save($dbConn = null)
94
    {
95
        if ($this->arrayField['id']) {
96
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
97
            if ($this->checkIfRecordIsEmpty($sqlData)) {
98
                $boolCommit = $this->DataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn);
99
                if ($boolCommit) {
100
                    return true;
101
                }
102
103
                throw new NoRecordUpdatedException('oops, your record did not update succesfully');
104
            }
105
106
            throw new EmptyArrayException("data passed didn't match any record");
107
        }
108
109
        $boolCommit = $this->DataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn);
110
        if ($boolCommit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $boolCommit of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
111
            
112
            return true;
113
        }
114
115
        throw new NoRecordCreatedException('oops,your record did not create succesfully');
116
    }
117
118
    /**
119
     * This method find a record by id.
120
     *
121
     * @param $id
122
     *
123
     * @throws ArgumentNumberIncorrectException
124
     * @throws ArgumentNotFoundException
125
     *
126
     * @return object
127
     */
128
    public static function findById($id)
129
    {
130
        $numArgs = func_num_args();
131
        if ($numArgs > 1) {
132
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
133
        }
134
        if ($numArgs == '') {
135
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
136
        }
137
138
        $staticFindInstance = new static();
139
        $staticFindInstance->id = $id == '' ? false : $id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Demo\DataBaseModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
140
        
141
        return $staticFindInstance;
142
    }
143
144
    /**
145
     * This method find a record by id and returns
146
     * all the data present in the id.
147
     *
148
     *
149
     * @return associative array
150
     */
151
    public function getById($dbConn = null)
152
    {
153
        if ($this->arrayField['id']) {
154
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
155
156
            return $sqlData;
157
        }
158
    }
159
160
    /**
161
     * This method delete a row from the table by the row id.
162
     *
163
     * @param $id
164
     *
165
     * @throws ArgumentNumberIncorrectException;
166
     * @throws ArgumentNotFoundException;
167
     *
168
     * @return bool true
169
     */
170 3
    public static function destroy($id, $dbConn)
171
    {
172 3
        $numArgs = func_num_args();
173 3
        if ($numArgs < 0 || $numArgs > 2) {
174
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
175
        }
176 3
        if ($numArgs == ' ') {
177
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
178
        }
179 3
        $sqlData = DataBaseQuery::read($id, self::getClassName(), $dbConn);
0 ignored issues
show
Unused Code introduced by
$sqlData 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...
180 3
        $sqlData = DataBaseQuery::delete($id, self::getClassName(), $dbConn);
0 ignored issues
show
Unused Code introduced by
$sqlData 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...
181
182 3
        return true;
183
    }
184
185 21
    public static function getClassName()
186
    {
187 21
        $tableName = explode('\\', get_called_class());
188
189 21
        return Inflector::pluralize(strtolower(end($tableName)));
190
    }
191
192
    /**
193
     * This method check if the argument passed to this function is an array.
194
     *
195
     * @param $arrayOfRecord
196
     *
197
     * @return bool true
198
     */
199 6
    public function checkIfRecordIsEmpty($arrayOfRecord)
200
    {
201 6
        if (count($arrayOfRecord) > 0) {
202 3
            return true;
203
        }
204
205 3
        return false;
206
    }
207
208
    /**
209
     * This method check if the argument passed to this function is an array.
210
     *
211
     * @param $arrayOfRecord
212
     *
213
     * @return bool true
214
     */
215
    public function checkIfRecordExist($arrayOfRecord)
216
    {
217
        $stringValue = [];
218
        foreach ($arrayOfRecord as $key => $val) {
219
            $stringValue[] = $val;
220
        }
221
    }
222
}
223