EntityRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 41
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 16 4
A build() 0 4 1
A buildOne() 0 4 1
A buildAll() 0 4 1
A getAlias() 0 6 1
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