Manager   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 107
ccs 31
cts 31
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 9 3
A getRepository() 0 4 1
A getObjectManager() 0 4 1
A setClass() 0 5 1
A getClass() 0 9 2
A create() 0 5 1
A delete() 0 5 1
A update() 0 8 2
1
<?php
2
3
namespace Loevgaard\DoctrineManager;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\Persistence\ObjectRepository;
8
9
/**
10
 * @method null|object find($id)
11
 * @method array findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null)
12
 * @method null|object findOneBy(array $criteria)
13
 * @method array findAll()
14
 * @method object persist($object)
15
 * @method flush()
16
 */
17
abstract class Manager
18
{
19
    /**
20
     * @var ManagerRegistry
21
     */
22
    protected $managerRegistry;
23
24
    /**
25
     * @var string
26
     */
27
    protected $class;
28
29
    public function __construct(ManagerRegistry $registry, string $class)
30
    {
31
        $this->managerRegistry = $registry;
32
        $this->class = $class;
33
    }
34 24
35
    /**
36 24
     * @param string $name
37 24
     * @param array $arguments
38 24
     * @return mixed
39
     */
40
    public function __call($name, $arguments)
41
    {
42
        if (method_exists($this->getRepository(), $name)) {
43
            return call_user_func_array([$this->getRepository(), $name], $arguments);
44
        }
45 3
        if (method_exists($this->getObjectManager(), $name)) {
46
            return call_user_func_array([$this->getObjectManager(), $name], $arguments);
47 3
        }
48 3
    }
49
50 3
    /**
51 3
     * @return ObjectRepository
52
     */
53 3
    public function getRepository() : ObjectRepository
54
    {
55
        return $this->getObjectManager()->getRepository($this->getClass());
56
    }
57
58 6
    /**
59
     * @return ObjectManager
60 6
     */
61
    public function getObjectManager() : ObjectManager
62
    {
63
        return $this->managerRegistry->getManagerForClass($this->class);
64
    }
65
66 15
    /**
67
     * @param string $class
68 15
     * @return Manager
69
     */
70
    public function setClass(string $class) : Manager
71
    {
72
        $this->class = $class;
73
        return $this;
74
    }
75 3
76
    /**
77 3
     * @return string
78 3
     */
79
    public function getClass() : string
80
    {
81
        if (false !== strpos($this->class, ':')) {
82
            $metadata = $this->getObjectManager()->getClassMetadata($this->class);
83
            $this->class = $metadata->getName();
84 15
        }
85
86 15
        return $this->class;
87 3
    }
88 3
89
    /**
90
     * Returns an instantiated entity class
91 15
     *
92
     * @return mixed
93
     */
94
    public function create()
95
    {
96
        $obj = new $this->class();
97
        return $obj;
98
    }
99 3
100
    /**
101 3
     * @param mixed $obj The entity
102 3
     */
103
    public function delete($obj)
104
    {
105
        $this->getObjectManager()->remove($obj);
106
        $this->getObjectManager()->flush();
107
    }
108 3
109
    /**
110 3
     * Will update/save the entity
111 3
     *
112 3
     * @param mixed $obj The entity
113
     * @param bool $flush
114
     */
115
    public function update($obj, $flush = true)
116
    {
117
        $this->getObjectManager()->persist($obj);
118
119
        if ($flush) {
120 3
            $this->getObjectManager()->flush();
121
        }
122 3
    }
123
}
124