Passed
Pull Request — master (#1)
by Михаил
07:07
created

EntityManager::flush()   D

Complexity

Conditions 9
Paths 23

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 23
nop 1
1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Manager;
4
5
use Doctrine\Bundle\DoctrineBundle\Registry;
6
use Doctrine\ORM\EntityManagerInterface;
7
8
class EntityManager
9
{
10
    /**
11
     * @var IndexProcessManager
12
     */
13
    private $indexProcessManager;
14
15
    /**
16
     * @var Registry
17
     */
18
    private $registry;
19
20
    /**
21
     * @param IndexProcessManager $indexProcessManager
22
     * @param Registry $registry
23
     */
24
    public function __construct(
25
        IndexProcessManager $indexProcessManager,
26
        Registry $registry
27
    ) {
28
        $this->indexProcessManager = $indexProcessManager;
29
        $this->registry = $registry;
30
    }
31
32
    /**
33
     * @param object[]|object $entity
34
     * @throws \Doctrine\ORM\ORMException
35
     * @throws \InvalidArgumentException
36
     * @throws \LogicException
37
     * @throws \Exception
38
     */
39
    public function flush($entity)
40
    {
41
        if (!is_object($entity) && !is_array($entity)) {
0 ignored issues
show
introduced by
The condition is_array($entity) is always true.
Loading history...
42
            throw new \InvalidArgumentException('Entity must be an object or array of objects');
43
        }
44
45
        if (!is_array($entity)) {
46
            $entity = [$entity];
47
        }
48
49
        foreach ($entity as $object) {
50
            if (!method_exists($object, 'getId')) {
51
                throw new \LogicException('Entity must have method "getId" to handle rollback');
52
            }
53
54
            /** @var \Doctrine\ORM\EntityManager $em */
55
            $em = $this->getEm($object);
56
            try {
57
                $em->persist($object);
58
                $em->flush($object);
59
            } catch (\Exception $e) {
60
                if (!$em->isOpen()) {
61
                    $em = $em->create(
62
                        $em->getConnection(), $em->getConfiguration()
63
                    );
64
                }
65
66
                $previousObject = $em->getRepository(get_class($object))->find($object->getId());
67
                if ($previousObject) {
68
                    $this->indexProcessManager->reindex($previousObject);
69
                } else {
70
                    $this->indexProcessManager->remove($object);
71
                }
72
73
                throw $e;
74
            }
75
        }
76
    }
77
78
    /**
79
     * @param $entity
80
     * @throws \InvalidArgumentException
81
     * @return EntityManagerInterface
82
     */
83
    private function getEm($entity)
84
    {
85
        $em = $this->registry->getManagerForClass(get_class($entity));
86
        if (!$em) {
87
            throw new \InvalidArgumentException(
88
                sprintf('There is no entity manager for "%s" class', get_class($entity))
89
            );
90
        }
91
92
        if (!$em instanceof EntityManagerInterface) {
93
            throw new \InvalidArgumentException(
94
                'Entity manager must be instance of  "EntityManagerInterface" class'
95
            );
96
        }
97
98
        return $em;
99
    }
100
}