Completed
Push — master ( ba5607...5fa440 )
by Adeola
02:28
created

DataBaseModel::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 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 39
    public function __construct($dbConn = null)
29
    {
30 39
        if (is_null($dbConn)) {
31
            $dbConn = $this->dataBaseConnection;
32
        }
33 39
        $this->tableName = self::getClassName();
34 39
        $this->dataBaseQuery = new DataBaseQuery($dbConn);
35 39
        $this->arrayField['id'] = 0;
36 39
    }
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
        return $sqlData;
76
    }
77
            
78
    /**
79
     * This method either create or update record in a database table
80
     * by calling either the read method or create method in the
81
     * DataBaseQuery class.
82
     *
83
     * @throws NoRecordUpdateException
84
     * @throws EmptyArrayException
85
     * @throws NoRecordCreatedException
86
     *
87
     * @return bool true or false;
88
     */
89 2
    public function save($dbConn = null)
90
    {
91
        if ($this->arrayField['id']) {
92
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
93
94
            if ($this->checkIfRecordIsEmpty($sqlData)) {
95
                $boolCommit = $this->dataBaseQuery->update(['id' => $this->arrayField['id']], $this->arrayField, self::getClassName(), $dbConn);
96
                var_dump($this->arrayField);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->arrayField); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
97
                if ($boolCommit) {
98
                    return true;
99 2
                }
100
101
                throw new NoRecordUpdatedException('oops, your record did not update succesfully');
102
            }
103
104
            throw new EmptyArrayException("data passed didn't match any record");
105
        } else {
106
107
            $boolCommit = $this->dataBaseQuery->create($this->arrayField, self::getClassName(), $dbConn);
108
109
            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...
110
                return true;
111
            }
112
113
            throw new NoRecordCreatedException('oops,your record did not create succesfully');
114
        }
115
   
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 6
    public static function findById($id)
129
    {
130 6
        $numArgs = func_num_args();
131
132 6
        if ($numArgs > 1) {
133 3
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
134
        }
135
136 3
        if ($id == '') {
137 3
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
138
        }
139
140
        $staticFindInstance = new static();
141
142
        $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...
143
144
        return $staticFindInstance;
145
    }
146
147
    /**
148
     * This method find a record by id and returns
149
     * all the data present in the id.
150
     *
151
     *
152
     * @return associative array
153
     */
154
    public function getById($dbConn = null)
155
    {
156
        if ($this->arrayField['id']) {
157
            $sqlData = DataBaseQuery::read($this->arrayField['id'], self::getClassName(), $dbConn);
158
        
159
            return $sqlData;
160
        }
161
    }
162
163
    /**
164
     * This method delete a row from the table by the row id.
165
     *
166
     * @param $id
167
     *
168
     * @throws ArgumentNumberIncorrectException;
169
     * @throws ArgumentNotFoundException;
170
     *
171
     * @return bool true
172
     */
173 9
    public static function destroy($id, $dbConn = null)
174
    {
175 9
        $numArgs = func_num_args();
176 9
        if ($numArgs > 2) {
177 3
            throw new ArgumentNumberIncorrectException('Please input just one Argument');
178
        }
179
180 6
        if ($numArgs == ' ') {
181 3
            throw new ArgumentNotFoundException('No Argument found, please input an argument');
182
        }
183
184 3
        DataBaseQuery::delete($id, self::getClassName(), $dbConn);
185
186 3
        return true;
187
    }
188
189 39
    public static function getClassName()
190
    {
191 39
        $tableName = explode('\\', get_called_class());
192
        
193 39
        return Inflector::pluralize(strtolower(end($tableName)));
194
    }
195
196
    /**
197
     * This method check if the argument passed to this function is an array.
198
     *
199
     * @param $arrayOfRecord
200
     *
201
     * @return bool true
202
     */
203 6
    public function checkIfRecordIsEmpty($arrayOfRecord)
204
    {
205 6
        if (count($arrayOfRecord) > 0) {
206 3
            return true;
207
        }
208
209 3
        return false;
210
    }
211
}
212