Completed
Push — master ( 8c968a...c71f44 )
by Rougin
04:50
created

CodeigniterModel::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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
class CodeigniterModel extends \CI_Model
12
{
13
    /**
14
     * @var \Rougin\Credo\Credo
15
     */
16
    protected $credo;
17
18
    /**
19
     * @var \Doctrine\ORM\EntityRepository
20
     */
21
    protected $repository;
22
23 3
    public function __construct()
24
    {
25 3
        parent::__construct();
26
27 3
        $this->credo      = new Credo($this->db);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Rougin\Credo\CodeigniterModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
28 3
        $this->repository = $this->credo->getRepository(get_class($this));
0 ignored issues
show
Documentation Bug introduced by
The method getRepository 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...
29 3
    }
30
31
    /**
32
     * Returns all of the models from the database.
33
     *
34
     * @return array
35
     */
36 3
    public function all()
37
    {
38 3
        return $this->find_by([]);
39
    }
40
41
    /**
42
     * Deletes the specified ID of the model from the database.
43
     *
44
     * @param  integer $id
45
     * @return void
46
     */
47 3
    public function delete($id)
48 3
    {
49 2
        $item = $this->find($id);
50
51 2
        $this->credo->remove($item);
0 ignored issues
show
Documentation Bug introduced by
The method remove 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...
52 2
        $this->credo->flush();
0 ignored issues
show
Documentation Bug introduced by
The method flush 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...
53 2
    }
54
55
    /**
56
     * Finds an entity by its primary key / identifier.
57
     *
58
     * @param  mixed    $id
59
     * @param  int|null $lockMode
60
     * @param  int|null $lockVersion
61
     * @return object|null
62
     */
63 4
    public function find($id, $lockMode = null, $lockVersion = null)
64
    {
65 4
        return $this->repository->find($id, $lockMode, $lockVersion);
66
    }
67
68
    /**
69
     * Finds models by a set of criteria.
70
     *
71
     * @param  array        $criteria
72
     * @param  array|null   $orderBy
73
     * @param  integer|null $limit
74
     * @param  integer|null $offset
75
     * @return array
76
     */
77 3
    public function find_by(array $criteria, array $orderBy = null, $limit = null, $offset = null)
78
    {
79 3
        return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
80
    }
81
}
82