Model::find()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
1
<?php
2
/**
3
 * PotatoORM manages the persistence of database CRUD operations.
4
 *
5
 * @package Ibonly\PotatoORM\Model
6
 * @author  Ibraheem ADENIYI <[email protected]>
7
 * @license MIT <https://opensource.org/licenses/MIT>
8
 */
9
10
namespace Ibonly\PotatoORM;
11
12
use PDO;
13
use Exception;
14
use PDOException;
15
use Ibonly\PotatoORM\GetData;
16
use Ibonly\PotatoORM\DataNotFoundException;
17
use Ibonly\PotatoORM\EmptyDatabaseException;
18
use Ibonly\PotatoORM\ColumnNotExistExeption;
19
use Ibonly\PotatoORM\DataAlreadyExistException;
20
use Ibonly\PotatoORM\InvalidConnectionException;
21
22
class Model extends Relationships implements ModelInterface, RelationshipsInterface
23
{
24
    /**
25
     * getALL()
26
     * Get all record from the database
27
     *
28
     * @return object
29
     */
30 View Code Duplication
    public function getAll($dbConnection = NULL)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $connection = DatabaseQuery::checkConnection($dbConnection);
33
34
        $sqlQuery = self::whereClause();
35
        $query = $connection->prepare($sqlQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
36
        $query->execute();
37
        if ($query->rowCount()) {
38
            return new GetData($query->fetchAll($connection::FETCH_ASSOC));
39
        }
40
        throw new EmptyDatabaseException();
41
    }
42
43
    /**
44
     * where($data, $condition)
45
     * Get data from database where $data = $condition
46
     *
47
     * @return object
48
     */
49
    public function where($data, $condition = NULL, $dbConnection = NULL)
50
    {
51
        $databaseQuery = new DatabaseQuery();
52
        $connection = $databaseQuery->checkConnection($dbConnection);
53
54
        $sqlQuery = self::whereClause($data, $condition, $connection);
55
        $query = $connection->prepare($sqlQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
56
        $query->execute();
57
        if ($query->rowCount()) {
58
            return new GetData($query->fetchAll($connection::FETCH_ASSOC));
59
        }
60
        throw new DataNotFoundException();
61
    }
62
63
    /**
64
     * find($value)
65
     * Find data from database where id = $value
66
     *
67
     * @return array
68
     */
69
    public static function find($value, $dbConnection = NULL)
70
    {
71
        $connection = DatabaseQuery::checkConnection($dbConnection);
72
73
        $sqlQuery = DatabaseQuery::selectQuery(self::getTableName($connection), self::fields(), ['id' => $value], NULL, $connection);
74
        $query = $connection->prepare($sqlQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
75
        $query->execute();
76
        if ($query->rowCount()) {
77
            $found = new static;
78
            $found->id = $value;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Ibonly\PotatoORM\Model>. 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...
79
            $found->data = $query->fetchAll($connection::FETCH_ASSOC);
0 ignored issues
show
Documentation introduced by
The property data does not exist on object<Ibonly\PotatoORM\Model>. 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...
80
            return $found;
81
        }
82
        throw new DataNotFoundException();
83
    }
84
85
    /**
86
     * save()
87
     * Insert data into database
88
     *
89
     * @return bool
90
     */
91 View Code Duplication
    public function save($dbConnection = NULL)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        $connection = DatabaseQuery::checkConnection($dbConnection);
94
95
        $query = $this->insertQuery(self::getTableName($connection));
96
        $statement = $connection->prepare($query);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
97
        if($statement->execute()) {
98
            return true;
99
        }
100
        throw new  DataAlreadyExistException();
101
102
    }
103
104
    /**
105
     * update()
106
     * Update details in database after ::find(2)
107
     *
108
     * @return bool
109
     */
110 View Code Duplication
    public function update($id, $dbConnection = NULL)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $connection = DatabaseQuery::checkConnection($dbConnection);
113
114
        $updateQuery = $this->updateQuery(self::getTableName($connection), $id);
115
        $statement = $connection->prepare($updateQuery);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
116
        if ($statement->execute()) {
117
            return true;
118
        }
119
        throw new  DataAlreadyExistException();
120
    }
121
122
    /**
123
     * destroy($value)
124
     * Delete data from database
125
     *
126
     * @return bool
127
     */
128
    public function destroy($value, $dbConnection = NULL)
129
    {
130
        $connection = DatabaseQuery::checkConnection($dbConnection);
131
132
        $query = $connection->prepare('DELETE FROM ' . self::getTableName($connection) . ' WHERE id = '.$value);
0 ignored issues
show
Bug introduced by
The method prepare cannot be called on $connection (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
133
        $query->execute();
134
        $check = $query->rowCount();
135
        if ($check) {
136
            return true;
137
        }
138
        throw new DataNotFoundException;
139
    }
140
}