Completed
Push — master ( b70b78...814e9e )
by Filipe
03:21
created

EntityRepository::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Repository;
11
12
use Slick\Database\Sql;
13
use Slick\Orm\EntityInterface;
14
use Slick\Orm\RepositoryInterface;
15
16
/**
17
 * Class EntityRepository
18
 *
19
 * @package Slick\Orm\Repository
20
 * @author  Filipe Silva <[email protected]>
21
 */
22
class EntityRepository extends AbstractRepository implements
23
    RepositoryInterface
24
{
25
    /**
26
     * @var IdentityMapInterface
27
     */
28
    protected $identityMap;
29
30
    /**
31
     * Gets an entity by its id
32
     *
33
     * @param mixed $entityId
34
     *
35
     * @return EntityInterface|null
36
     */
37
    public function get($entityId)
38
    {
39
        $entity = $this->getIdentityMap()->get($entityId, false);
40
        if ($entity === false) {
41
            $entity = $this->load($entity);
42
        }
43
        return $entity;
44
    }
45
46
    /**
47
     * Sets identity map for this repository
48
     *
49
     * @param IdentityMapInterface $map
50
     * @return $this|self|EntityRepository
51
     */
52
    public function setIdentityMap(IdentityMapInterface $map)
53
    {
54
        $this->identityMap = $map;
55
        return $this;
56
    }
57
58
    /**
59
     * Gets identity map for this repository
60
     *
61
     * @return IdentityMapInterface
62
     */
63
    protected function getIdentityMap()
64
    {
65
        if (null == $this->identityMap) {
66
            $this->setIdentityMap(new IdentityMap());
67
        }
68
        return $this->identityMap;
69
    }
70
71
    /**
72
     * Loads entity from database
73
     *
74
     * @param $entityId
75
     *
76
     * @return null|EntityInterface
77
     */
78
    protected function load($entityId)
79
    {
80
        $table = $this->getEntityDescriptor()->getTableName();
81
        $primaryKey = $this->getEntityDescriptor()
82
            ->getPrimaryKey()
83
            ->getField();
84
        $data = Sql::createSql($this->getAdapter())
85
            ->select($this->getEntityDescriptor()->getTableName())
86
            ->where(["{$table}.{$primaryKey} = ?" => $entityId])
0 ignored issues
show
Documentation introduced by
array("{$table}.{$primaryKey} = ?" => $entityId) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
            ->first();
88
        $entity = null;
89
        if ($data) {
90
            $class = $this->getEntityDescriptor()->className();
91
            $entity = new $class($data);
92
            $this->getIdentityMap()->set($entity);
93
        }
94
        return $entity;
95
    }
96
}