Completed
Push — master ( 3e3108...5e7e99 )
by Adeola
02:33
created

DataBaseModel::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 11
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0078
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 42
    public function __construct($dbConn = null)
29
    {
30 42
        if (is_null($dbConn)) {
31 3
            $dbConn = new DataBaseQuery(null);
0 ignored issues
show
Unused Code introduced by
$dbConn 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...
32
        } else {
33 42
            $this->dataBaseQuery = $dbConn; 
34
        }
35
36 42
        $this->tableName = self::getClassName();
37 42
        $this->arrayField['id'] = 0;
38 42
    }
39
40
    /**
41
     * The magic setter method.
42
     *
43
     * @param $properties
44
     * @param $values
45
     *
46
     * @return array associative array properties
47
     */
48
    public function __set($properties, $values)
49
    {
50
        $this->arrayField[$properties] = $values;
51
    }
52
53
    /**
54
     * The magic getter method.
55
     *
56
     * @param $properties
57
     *
58
     * @return array key
59
     */
60
    public function __get($properties)
61
    {
62
        return $this->arrayField[$properties];
63
    }
64
65
    /**
66
     * This method gets all the record from a particular table
67
     * by accessing the read method from the DataBaseQuery class.
68
     *
69
     * @throws NoDataFoundException
70
     *
71
     * @return associative array
72
     */
73 6
    public static function getAll($dbConn = null)
74
    {
75 6
        $sqlData = DataBaseQuery::read($id = false, self::getClassName(), $dbConn);
76
77 3
        return $sqlData;
78
    }
79
            
80
    /**
81
     * This method either create or update record in a database table
82
     * by calling either the read method or create method in the
83
     * DataBaseQuery class.
84
     *
85
     * @throws NoRecordUpdateException
86
     * @throws EmptyArrayException
87
     * @throws NoRecordCreatedException
88
     *
89
     * @return bool true or false;
90
     */
91 2
    public function save($dbConn = null)
92
    {
93
        if ($this->arrayField['id']) {
94
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
95
96
            if ($this->checkIfRecordIsEmpty($sqlData)) {
97
                $boolCommit = $this->dataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn);
98
    
99 2
                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
        } else {
108
109
            $boolCommit = $this->dataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn);
110
111
            if ($boolCommit) {
112
                return true;
113
            }
114
115
            throw new NoRecordCreatedException('oops,your record did not create succesfully');
116
        }
117
   
118
    }
119
120
    /**
121
     * This method find a record by id.
122
     *
123
     * @param $id
124
     *
125
     * @throws ArgumentNumberIncorrectException
126
     * @throws ArgumentNotFoundException
127
     *
128
     * @return object
129
     */
130 9
    public static function findById($id)
131
    {
132 9
        $numArgs = func_num_args();
133
134 9
        if ($numArgs > 1) {
135 3
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
136
        }
137
138 6
        if ($id == '') {
139 3
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
140
        }
141
142 3
        $staticFindInstance = new static();
143
144
        $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...
145
146
        return $staticFindInstance;
147
    }
148
149
    /**
150
     * This method find a record by id and returns
151
     * all the data present in the id.
152
     *
153
     *
154
     * @return associative array
155
     */
156
    public function getById($dbConn = null)
157
    {
158
        if ($this->arrayField['id']) {
159
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
160
        
161
            return $sqlData;
162
        }
163
    }
164
165
    /**
166
     * This method delete a row from the table by the row id.
167
     *
168
     * @param $id
169
     *
170
     * @throws ArgumentNumberIncorrectException;
171
     * @throws ArgumentNotFoundException;
172
     *
173
     * @return bool true
174
     */
175 9
    public static function destroy($id, $dbConn = null)
176
    {
177 9
        $numArgs = func_num_args();
178 9
        if ($numArgs > 2) {
179 3
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
180
        }
181
182 6
        if ($numArgs == ' ') {
183 3
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
184
        }
185
186 3
        DataBaseQuery::delete($id, self::getClassName(), $dbConn);
187
188 3
        return true;
189
    }
190
191 42
    public static function getClassName()
192
    {
193 42
        $tableName = explode('\\', get_called_class());
194
        
195 42
        return Inflector::pluralize(strtolower(end($tableName)));
196
    }
197
198
    /**
199
     * This method check if the argument passed to this function is an array.
200
     *
201
     * @param $arrayOfRecord
202
     *
203
     * @return bool true
204
     */
205 6
    public function checkIfRecordIsEmpty($arrayOfRecord)
206
    {
207 6
        if (count($arrayOfRecord) > 0) {
208 3
            return true;
209
        }
210
211 3
        return false;
212
    }
213
}
214