EntityRepository::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Knp\RadBundle\Doctrine;
4
5
use Doctrine\ORM\EntityRepository as BaseEntityRepository;
6
use Doctrine\Common\Util\Inflector;
7
8
class EntityRepository extends BaseEntityRepository
9
{
10
    public function __call($method, $arguments)
11
    {
12
        if (0 === strpos($method, 'find')) {
13
            if (method_exists($this, $builder = 'build'.substr($method, 4))) {
14
                $qb = call_user_func_array(array($this, $builder), $arguments);
15
16
                if (0 === strpos(substr($method, 4), 'One')) {
17
                    return $qb->getQuery()->getOneOrNullResult();
18
                }
19
20
                return $qb->getQuery()->getResult();
21
            }
22
        }
23
24
        return parent::__call($method, $arguments);
25
    }
26
27
    protected function build()
28
    {
29
        return $this->createQueryBuilder($this->getAlias());
30
    }
31
32
    protected function buildOne($id)
33
    {
34
        return $this->build()->where($this->getAlias().'.id = '.intval($id));
35
    }
36
37
    protected function buildAll()
38
    {
39
        return $this->build();
40
    }
41
42
    protected function getAlias()
43
    {
44
        $name = basename(str_replace('\\', '/', $this->getClassName()));
45
46
        return Inflector::tableize($name);
47
    }
48
}
49