Completed
Push — master ( 901465...46d7dd )
by Adeola
02:39
created

DataBaseModel::throwNoRecordCreatedException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
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 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
     * @throws NoDataFoundException
65
     *
66
     * @return associative array
67
     */
68
    public static function getAll($dbConn)
69
    {
70
        $sqlData = DataBaseQuery::read($id = false, self::getClassName(), $dbConn);
71
72 6
        return $sqlData;
73
    }
74 6
            
75
    /**
76 3
     * This method either create or update record in a database table
77
     * by calling either the read method or create method in the
78
     * DataBaseQuery class.
79
     *
80
     * @throws NoRecordUpdateException
81
     * @throws EmptyArrayException
82
     * @throws NoRecordCreatedException
83
     *
84
     * @return bool true or false;
85
     */
86
    public function save($dbConn)
87
    {
88
        if ($this->arrayField['id']) {
89
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
90 2
91
            if ($this->checkIfRecordIsEmpty($sqlData)) {
92
                $boolCommit = $this->dataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn);
93
    
94
                if ($boolCommit) {
95
                    return true;
96
                }
97
98
                $this->throwNoRecordUpdatedException();
99 2
            }
100
101
            $this->throwEmptyArrayException();
102
        } else {
103
104
            $boolCommit = $this->dataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn);
105
106
            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...
107
                return true;
108
            }
109
110
            $this->throwNoRecordCreatedException();//throw new NoRecordCreatedException('oops,your record did not create succesfully');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
        }
112
   
113
    }
114
115
    public function throwNoRecordUpdatedException()
116
    {
117
        $message = "oops, your record did not update succesfully";
118
        throw new NoRecordUpdatedException($message);
119
    }
120
121
    public function throwEmptyArrayException()
122
    {
123
        $message = "data passed didn't match any record";
124
        throw new EmptyArrayException($message);
125
    }
126
127
    public function throwNoRecordCreatedException()
128
    {
129 9
        $message = "oops,your record did not create succesfully";
130
        throw new NoRecordCreatedException($message);
131 9
    }
132
133 9
    /**
134 3
     * This method find a record by id.
135
     *
136
     * @param $id
137 6
     *
138 3
     * @throws ArgumentNumberIncorrectException
139
     * @throws ArgumentNotFoundException
140
     *
141 3
     * @return object
142
     */
143
    public static function findById($id)
144
    {
145
        $numArgs = func_num_args();
146
147
        if ($numArgs > 1) {
148
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
149
        }
150
151
        if ($id == '') {
152
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
153
        }
154
155
        $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...
156
157
        $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...
158
159
        return $staticFindInstance;
160
    }
161
162
    /**
163
     * This method find a record by id and returns
164
     * all the data present in the id.
165
     *
166
     *
167
     * @return associative array
168
     */
169
    public function getById()
170
    {
171
        if ($this->arrayField['id']) {
172
            $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...
173
        
174 9
            return $sqlData;
175
        }
176 9
    }
177 9
178 3
    /**
179
     * This method delete a row from the table by the row id.
180
     *
181 6
     * @param $id
182 3
     *
183
     * @throws ArgumentNumberIncorrectException;
184
     * @throws ArgumentNotFoundException;
185 3
     *
186
     * @return bool true
187 3
     */
188
    public static function destroy($id, $dbConn)
189
    {
190 42
        $numArgs = func_num_args();
191
        if ($numArgs > 2) {
192 42
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
193
        }
194 42
195
        if ($numArgs == ' ') {
196
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
197
        }
198
199
        DataBaseQuery::delete($id, self::getClassName(), $dbConn);
200
201
        return true;
202
    }
203
204 6
    public static function getClassName()
205
    {
206 6
        $tableName = explode('\\', get_called_class());
207 3
        
208
        return Inflector::pluralize(strtolower(end($tableName)));
209
    }
210 3
211
    /**
212
     * This method check if the argument passed to this function is an array.
213
     *
214
     * @param $arrayOfRecord
215
     *
216
     * @return bool true
217
     */
218
    public function checkIfRecordIsEmpty($arrayOfRecord)
219
    {
220
        if (count($arrayOfRecord) > 0) {
221
            return true;
222
        }
223
224
        return false;
225
    }
226
}
227