Passed
Push — master ( e293fe...fcb2e7 )
by Rougin
06:31
created

Model::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rougin\Credo;
4
5
/**
6
 * Model
7
 *
8
 * @package Credo
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 *
11
 * @property \CI_DB_query_builder $db
12
 */
13
class Model extends \CI_Model
14
{
15
    use Traits\CredoTrait;
16
17
    /**
18
     * Deletes the specified ID of the model from the database.
19
     *
20
     * @param  integer $id
21
     * @return void
22
     */
23 3
    public function delete($id)
24
    {
25 3
        $item = $this->find($id);
26
27 3
        $this->credo->remove($item);
28
29 3
        $this->credo->flush();
30 3
    }
31
32
    /**
33
     * Inserts a new row into the table.
34
     *
35
     * @param  array $data
36
     * @return integer
37
     */
38 3
    public function insert(array $data)
39
    {
40 3
        $metadata = $this->metadata((string) get_class($this));
41
42 3
        $this->db->insert($metadata->getTableName(), $data);
43
44 3
        $item = $this->find($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()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $item = $this->find($lastId = $this->db->/** @scrutinizer ignore-call */ insert_id());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
46 3
        $this->credo->refresh($item);
47
48 3
        return $lastId;
49
    }
50
51
    /**
52
     * Updates the selected row from the table.
53
     *
54
     * @param  integer $id
55
     * @param  array   $data
56
     * @return boolean
57
     */
58 3
    public function update($id, array $data)
59
    {
60 3
        $metadata = $this->metadata((string) get_class($this));
61
62 3
        $primary = $metadata->getSingleIdentifierColumnName();
63
64 3
        $this->db->where($primary, $id)->set((array) $data);
65
66 3
        $result = $this->db->update($metadata->getTableName());
67
68 3
        $this->credo->refresh($this->find((integer) $id));
69
70 3
        return $result;
71
    }
72
73
    /**
74
     * Returns the metadata of an entity.
75
     *
76
     * @param  string $class
77
     * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata
78
     */
79 6
    protected function metadata($class)
80
    {
81 6
        return $this->credo->getMetadataFactory()->getMetadataFor($class);
82
    }
83
}
84