Doctrine   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A persist() 0 8 2
A remove() 0 8 2
A flush() 0 4 1
B findBy() 0 18 8
A findOr404() 0 8 2
A getManager() 0 4 1
A getRepository() 0 4 2
1
<?php
2
3
namespace Knp\RadBundle\Controller\Helper;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
class Doctrine
10
{
11
    private $doctrine;
12
13
    public function __construct(ManagerRegistry $doctrine)
14
    {
15
        $this->doctrine = $doctrine;
16
    }
17
18
    public function persist($object, $flush = false)
19
    {
20
        $this->getManager()->persist($object);
21
22
        if ($flush) {
23
            $this->flush($object);
24
        }
25
    }
26
27
    public function remove($object, $flush = false)
28
    {
29
        $this->getManager()->remove($object);
30
31
        if ($flush) {
32
            $this->flush();
33
        }
34
    }
35
36
    public function flush($object = null)
37
    {
38
        $this->getManager()->flush($object);
39
    }
40
41
    public function findBy($object, $criterias = array())
42
    {
43
        $result = null;
44
        $findMethod = is_scalar($criterias) ? 'find' : 'findOneBy';
45
46
        if (is_object($object) && $object instanceof ObjectRepository) {
47
            $result = $object->$findMethod($criterias);
48
        } elseif (is_object($object) && $this->getManager()->contains($object)) {
49
            $result = $this->getManager()->refresh($object);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->getManager()->refresh($object) (which targets Doctrine\Common\Persiste...bjectManager::refresh()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50
        } elseif (is_string($object)) {
51
            $repository = $this->getRepository($object);
52
            $result = $repository->$findMethod($criterias);
53
        }
54
55
        if (null !== $result) {
56
            return $result;
57
        }
58
    }
59
60
    public function findOr404($object, $criterias = array())
61
    {
62
        if ($result = $this->findBy($object, $criterias)) {
63
            return $result;
64
        }
65
66
        throw new NotFoundHttpException;
67
    }
68
69
    public function getManager()
70
    {
71
        return $this->doctrine->getManager();
72
    }
73
74
    public function getRepository($object)
75
    {
76
        return $this->getManager()->getRepository(is_object($object) ? get_class($object) : $object);
77
    }
78
}
79