Completed
Push — master ( 642b77...e49431 )
by Adeola
02:35
created

DataBaseModel   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 63.79%

Importance

Changes 31
Bugs 4 Features 4
Metric Value
wmc 25
c 31
b 4
f 4
lcom 1
cbo 8
dl 0
loc 220
ccs 37
cts 58
cp 0.6379
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 4 1
A __get() 0 4 1
A __construct() 0 6 1
A getAll() 0 11 2
A throwNoDataFoundException() 0 5 1
B save() 0 28 5
A throwNoRecordUpdatedException() 0 5 1
A throwEmptyArrayException() 0 5 1
A throwNoRecordCreatedException() 0 5 1
A findById() 0 18 3
A getById() 0 8 2
A destroy() 0 15 3
A getClassName() 0 6 1
A checkIfRecordIsEmpty() 0 8 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 $dbConn;
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 42
    public function __construct($dbConn)
29
    {
30 42
        $this->tableName = self::getClassName();
31 3
        $this->dataBaseQuery = new DataBaseQuery($dbConn);
32
        $this->arrayField['id'] = 0;
33 42
    }
34
35 42
    /**
36 42
     * The magic setter method.
37 42
     *
38
     * @param $properties
39
     * @param $values
40
     *
41
     * @return array associative array properties
42
     */
43
    public function __set($properties, $values)
44
    {
45
        $this->arrayField[$properties] = $values;
46
    }
47 42
48
    /**
49 42
     * The magic getter method.
50 42
     *
51
     * @param $properties
52
     *
53
     * @return array key
54
     */
55
    public function __get($properties)
56
    {
57
        return $this->arrayField[$properties];
58
    }
59
60
    /**
61
     * This method gets all the record from a particular table
62
     * by accessing the read method from the DataBaseQuery class.
63
     *
64
     *
65
     * @return associative array
66
     */
67
    public static function getAll($dbConn)
68
    {
69
70
        $sqlData = DataBaseQuery::read($id = false, self::getClassName(), $dbConn);
71
72 6
        if (count($sqlData) > 0) {
73
            return $sqlData;
74 6
        }
75
        
76 3
        self::throwNoDataFoundException();
77
    }
78
79
    public static function throwNoDataFoundException()
80
    {
81
        $message = "oops, no data found in the database";
82
        throw new NoDataFoundException($message);
83
    }
84
            
85
    /**
86
     * This method either create or update record in a database table
87
     * by calling either the read method or create method in the
88
     * DataBaseQuery class.
89
     *
90 2
     * @throws NoRecordUpdateException
91
     * @throws EmptyArrayException
92
     * @throws NoRecordCreatedException
93
     *
94
     * @return bool true or false;
95
     */
96
    public function save($dbConn)
97
    {
98
        if ($this->arrayField['id']) {
99 2
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
100
101
            if ($this->checkIfRecordIsEmpty($sqlData)) {
102
                $boolCommit = $this->dataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn);
103
    
104
                if ($boolCommit) {
105
                    return true;
106
                }
107
108
                $this->throwNoRecordUpdatedException();
109
            }
110
111
            $this->throwEmptyArrayException();
112
        } else {
113
114
            $boolCommit = $this->dataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn);
115
116
            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...
117
                return true;
118
            }
119
120
            $this->throwNoRecordCreatedException();
121
        }
122
   
123
    }
124
125
    public function throwNoRecordUpdatedException()
126
    {
127
        $message = "oops, your record did not update succesfully";
128
        throw new NoRecordUpdatedException($message);
129 9
    }
130
131 9
    public function throwEmptyArrayException()
132
    {
133 9
        $message = "data passed didn't match any record";
134 3
        throw new EmptyArrayException($message);
135
    }
136
137 6
    public function throwNoRecordCreatedException()
138 3
    {
139
        $message = "oops,your record did not create succesfully";
140
        throw new NoRecordCreatedException($message);
141 3
    }
142
143
    /**
144
     * This method find a record by id.
145
     *
146
     * @param $id
147
     *
148
     * @throws ArgumentNumberIncorrectException
149
     * @throws ArgumentNotFoundException
150
     *
151
     * @return object
152
     */
153
    public static function findById($id)
154
    {
155
        $numArgs = func_num_args();
156
157
        if ($numArgs > 1) {
158
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
159
        }
160
161
        if ($id == '') {
162
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
163
        }
164
165
        $staticFindInstance = new static();
0 ignored issues
show
Bug introduced by
The call to DataBaseModel::__construct() misses a required argument $dbConn.

This check looks for function calls that miss required arguments.

Loading history...
166
167
        $staticFindInstance->id = $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...
168
169
        return $staticFindInstance;
170
    }
171
172
    /**
173
     * This method find a record by id and returns
174 9
     * all the data present in the id.
175
     *
176 9
     *
177 9
     * @return associative array
178 3
     */
179
    public function getById()
180
    {
181 6
        if ($this->arrayField['id']) {
182 3
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
0 ignored issues
show
Bug introduced by
The variable $dbConn does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
183
        
184
            return $sqlData;
185 3
        }
186
    }
187 3
188
    /**
189
     * This method delete a row from the table by the row id.
190 42
     *
191
     * @param $id
192 42
     *
193
     * @throws ArgumentNumberIncorrectException;
194 42
     * @throws ArgumentNotFoundException;
195
     *
196
     * @return bool true
197
     */
198
    public static function destroy($id, $dbConn)
199
    {
200
        $numArgs = func_num_args();
201
        if ($numArgs > 2) {
202
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
203
        }
204 6
205
        if ($numArgs == ' ') {
206 6
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
207 3
        }
208
209
        DataBaseQuery::delete($id, self::getClassName(), $dbConn);
210 3
211
        return true;
212
    }
213
214
    public static function getClassName()
215
    {
216
        $tableName = explode('\\', get_called_class());
217
        
218
        return Inflector::pluralize(strtolower(end($tableName)));
219
    }
220
221
    /**
222
     * This method check if the argument passed to this function is an array.
223
     *
224
     * @param $arrayOfRecord
225
     *
226
     * @return bool true
227
     */
228
    public function checkIfRecordIsEmpty($arrayOfRecord)
229
    {
230
        if (count($arrayOfRecord) > 0) {
231
            return true;
232
        }
233
234
        return false;
235
    }
236
}
237