Completed
Push — master ( b86bcd...3e8326 )
by Rougin
03:27
created

CodeigniterModel::getTableNameAndPrimaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rougin\Credo;
4
5
/**
6
 * Codeigniter Model
7
 *
8
 * @package Credo
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 *
11
 * @property \CI_DB_query_builder $db
12
 */
13
class CodeigniterModel extends \CI_Model
14
{
15
    /**
16
     * @var \Rougin\Credo\Credo
17
     */
18
    protected $credo;
19
20
    /**
21
     * @var \Doctrine\ORM\EntityRepository
22
     */
23
    protected $repository;
24
25 3
    public function __construct()
26
    {
27 3
        parent::__construct();
28
29 3
        $this->credo      = new Credo($this->db);
30 3
        $this->repository = $this->credo->getRepository(get_class($this));
31 3
    }
32
33
    /**
34
     * Returns all of the models from the database.
35
     *
36
     * @return array
37
     */
38 3
    public function all()
39
    {
40 3
        return $this->findBy([]);
41 3
    }
42
43
    /**
44
     * Returns a total rows from the specified table.
45
     *
46
     * @return integer
47
     */
48 3
    public function countAll()
49
    {
50 3
        list($tableName, $primaryKey) = $this->getTableNameAndPrimaryKey();
51
52 3
        $queryBuilder = $this->credo->createQueryBuilder();
53
54 3
        $queryBuilder->select($queryBuilder->expr()->count($tableName . '.' . $primaryKey));
55 3
        $queryBuilder->from(get_class($this), $tableName);
56
57 3
        return $queryBuilder->getQuery()->getSingleScalarResult();
58
    }
59
60
    /**
61
     * Deletes the specified ID of the model from the database.
62
     *
63
     * @param  integer $id
64
     * @return void
65
     */
66 3
    public function delete($id)
67
    {
68 3
        $item = $this->find($id);
69
70 3
        if (! is_null($item)) {
71 3
            $this->credo->remove($item);
72 3
            $this->credo->flush();
73 3
        }
74 3
    }
75
76
    /**
77
     * Finds an entity by its primary key / identifier.
78
     *
79
     * @param  mixed    $id
80
     * @param  int|null $lockMode
81
     * @param  int|null $lockVersion
82
     * @return object|null
83
     */
84 9
    public function find($id, $lockMode = null, $lockVersion = null)
85
    {
86 9
        return $this->repository->find($id, $lockMode, $lockVersion);
87 3
    }
88
89
    /**
90
     * Finds models by a set of criteria.
91
     *
92
     * @param  array        $criteria
93
     * @param  array|null   $orderBy
94
     * @param  integer|null $limit
95
     * @param  integer|null $offset
96
     * @return array
97
     */
98 6
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
99
    {
100 6
        return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
101
    }
102
103
    /**
104
     * Inserts a new row into the table.
105
     *
106
     * @param  array $data
107
     * @return integer
108
     */
109 3
    public function insert(array $data)
110
    {
111 3
        list($tableName) = $this->getTableNameAndPrimaryKey();
112
113 3
        $this->db->insert($tableName, $data);
114
115 3
        $lastId = $this->db->insert_id();
0 ignored issues
show
Bug introduced by
The method insert_id() does not exist on CI_DB_query_builder. Did you maybe mean insert()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116
117 3
        $this->credo->refresh($this->find($lastId));
0 ignored issues
show
Documentation Bug introduced by
The method refresh does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
118
119 3
        return $lastId;
120
    }
121
122
    /**
123
     * Updates the selected row from the table.
124
     *
125
     * @param  integer $id
126
     * @param  array   $data
127
     * @return boolean
128
     */
129 3
    public function update($id, array $data)
130
    {
131 3
        list($tableName, $primaryKey) = $this->getTableNameAndPrimaryKey();
132
133 3
        $this->db->where($primaryKey, $id);
134 3
        $this->db->set($data);
135
136 3
        $result = $this->db->update($tableName);
137
138 3
        $this->credo->refresh($this->find($id));
0 ignored issues
show
Documentation Bug introduced by
The method refresh does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
139
140 3
        return $result;
141
    }
142
143
    /**
144
     * Returns the table name and the corresponding primary key.
145
     *
146
     * @return array
147
     */
148 9
    protected function getTableNameAndPrimaryKey()
149
    {
150 9
        $factory  = $this->credo->getMetadataFactory();
151 9
        $metadata = $factory->getMetadataFor(get_class($this));
152
153 9
        return [ $metadata->getTableName(), $metadata->getSingleIdentifierColumnName() ];
154
    }
155
}
156