Completed
Push — master ( 5f250d...278b20 )
by Adeola
02:35
created

DataBaseModel   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 63.79%

Importance

Changes 23
Bugs 3 Features 3
Metric Value
c 23
b 3
f 3
dl 0
loc 196
wmc 21
lcom 1
cbo 8
ccs 37
cts 58
cp 0.6379
rs 10

10 Methods

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