Completed
Push — master ( e49431...97890f )
by Adeola
02:51
created

DataBaseModel::throwDataEmptyException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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 54
    public function __construct($dbConn)
29
    {
30 54
        $this->tableName = self::getClassName();
31 54
        $this->dataBaseQuery = new DataBaseQuery($dbConn);
32 54
        $this->arrayField['id'] = 0;
33 54
    }
34
35
    /**
36
     * The magic setter method.
37
     *
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
48
    /**
49
     * The magic getter method.
50
     *
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 6
    public static function getAll($dbConn)
68
    {
69 6
        $sqlData = DataBaseQuery::read($id = false, self::getClassName(), $dbConn);
70
71 3
        if (count($sqlData) > 0) {
72 3
            return $sqlData;
73
        }
74
75
        self::throwNoDataFoundException();
76
    }
77
78 3
    public static function throwNoDataFoundException()
79
    {
80 3
        $message = "oops, no data found in the database";
81 3
        throw new NoDataFoundException($message);
82
    }
83
            
84
    /**
85
     * This method either create or update record in a database table
86
     * by calling either the read method or create method in the
87
     * DataBaseQuery class.
88
     *
89
     * @throws NoRecordUpdateException
90
     * @throws EmptyArrayException
91
     * @throws NoRecordCreatedException
92
     *
93
     * @return bool true or false;
94
     */
95
    public function save($dbConn)
96
    {
97
        if ($this->arrayField['id']) {
98
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
99
100
            if ($this->checkIfRecordIsEmpty($sqlData)) {
101
                $boolCommit = $this->dataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn);
102
    
103
                if ($boolCommit) {
104
                    return true;
105
                }
106
107
                $this->throwNoRecordUpdatedException();
108
            }
109
110
            $this->throwEmptyArrayException();
111
        } else {
112
113
            $boolCommit = $this->dataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn);
114
115
            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...
116
                return true;
117
            }
118
119
            $this->throwNoRecordCreatedException();
120
        }
121
   
122
    }
123
124 3
    public function throwNoRecordUpdatedException()
125
    {
126 3
        $message = "oops, your record did not update succesfully";
127 3
        throw new NoRecordUpdatedException($message);
128
    }
129
130 3
    public function throwEmptyArrayException()
131
    {
132 3
        $message = "data passed didn't match any record";
133 3
        throw new EmptyArrayException($message);
134
    }
135
136 3
    public function throwNoRecordCreatedException()
137
    {
138 3
        $message = "oops,your record did not create succesfully";
139 3
        throw new NoRecordCreatedException($message);
140
    }
141
142
    /**
143
     * This method find a record by id.
144
     *
145
     * @param $id
146
     *
147
     * @throws ArgumentNumberIncorrectException
148
     * @throws ArgumentNotFoundException
149
     *
150
     * @return object
151
     */
152 6
    public static function findById($id)
153
    {
154 6
        $numArgs = func_num_args();
155
156 6
        if ($numArgs > 1) {
157 3
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
158
        }
159
160 3
        if ($id == '') {
161 3
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
162
        }
163
164
        $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...
165
166
        $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...
167
168
        return $staticFindInstance;
169
    }
170
171
    /**
172
     * This method find a record by id and returns
173
     * all the data present in the id.
174
     *
175
     *
176
     * @return associative array
177
     */
178
    public function getById()
179
    {
180
        if ($this->arrayField['id']) {
181
            $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...
182
            
183
            if (count($sqlData) > 0) {
184
                return $sqlData;
185
            }
186
187
            self::throwDataEmptyException();
188
        }
189
    }
190
191 3
    public function throwDataEmptyException()
192
    {
193 3
        $message = "oops, no data found in the column";
194 3
        throw new DataEmptyException($message);
195
    }
196
197
    /**
198
     * This method delete a row from the table by the row id.
199
     *
200
     * @param $id
201
     *
202
     * @throws ArgumentNumberIncorrectException;
203
     * @throws ArgumentNotFoundException;
204
     *
205
     * @return bool true
206
     */
207 9
    public static function destroy($id, $dbConn)
208
    {
209 9
        $numArgs = func_num_args();
210 9
        if ($numArgs > 2) {
211 3
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
212
        }
213
214 6
        if ($numArgs == ' ') {
215 3
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
216
        }
217
218 3
        DataBaseQuery::delete($id, self::getClassName(), $dbConn);
219
220 3
        return true;
221
    }
222
223 54
    public static function getClassName()
224
    {
225 54
        $tableName = explode('\\', get_called_class());
226
        
227 54
        return Inflector::pluralize(strtolower(end($tableName)));
228
    }
229
230
    /**
231
     * This method check if the argument passed to this function is an array.
232
     *
233
     * @param $arrayOfRecord
234
     *
235
     * @return bool true
236
     */
237 6
    public function checkIfRecordIsEmpty($arrayOfRecord)
238
    {
239 6
        if (count($arrayOfRecord) > 0) {
240 3
            return true;
241
        }
242
243 3
        return false;
244
    }
245
}
246