Passed
Push — master ( ee025d...83bd4a )
by Hirofumi
02:15
created

HandlesMultipleEntityManagers::flush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shippinno\DoctrineToolbelt;
5
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\ConnectionException;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\ORMException;
11
use Throwable;
12
13
trait HandlesMultipleEntityManagers
14
{
15
    /**
16
     * @var ManagerRegistry
17
     */
18
    protected $managerRegistry;
19
20
    /**
21
     * @param ManagerRegistry $managerRegistry
22
     */
23 4
    protected function setManagerRegistry(ManagerRegistry $managerRegistry): void
24
    {
25 4
        $this->managerRegistry = $managerRegistry;
26 4
    }
27
28
    /**
29
     * @param string[] $names
30
     * @throws RollbackException
31
     */
32 4
    public function flushManagersAtomically(array $names): void
33
    {
34 4
        $this->beginTransactions($names);
35
        try {
36
            try {
37 4
                $this->flush($names);
38 3
                $this->commit($names);
39 2
                return;
40 2
            } catch (Throwable $e) {
41 2
                $this->rollback($names);
42 1
                throw new RollbackException($e);
43
            }
44 2
        } catch (ConnectionException $e) {
45 1
            throw new RollbackFailedException($e);
46
        }
47
    }
48
49
    /**
50
     * @throws RollbackException
51
     */
52 4
    public function flushAllManagersAtomically(): void
53
    {
54 4
        $this->flushManagersAtomically($this->managerRegistry->getManagerNames());
55 2
    }
56
57
    /**
58
     * @param string[] $names
59
     * @throws ORMException
60
     */
61 4
    protected function flush(array $names): void
62
    {
63 4
        foreach ($names as $name) {
64 4
            $this->getEntityManager($name)->flush();
65
        }
66 3
    }
67
68
    /**
69
     * @param string[] $names
70
     */
71 4
    protected function beginTransactions(array $names): void
72
    {
73 4
        foreach ($names as $name) {
74 4
            $this->getConnection($name)->beginTransaction();
75
        }
76 4
    }
77
78
    /**
79
     * @param string[] $names
80
     * @throws ConnectionException
81
     */
82 3
    protected function commit(array $names): void
83
    {
84 3
        foreach ($names as $name) {
85 3
            $this->getConnection($name)->commit();
86
        }
87 2
    }
88
89
    /**
90
     * @param string[] $names
91
     * @throws ConnectionException
92
     */
93 2
    protected function rollback(array $names): void
94
    {
95 2
        foreach ($names as $name) {
96 2
            $this->getConnection($name)->rollBack();
97
        }
98 1
    }
99
100
    /**
101
     * @param string $name
102
     * @return EntityManager
103
     */
104 4
    protected function getEntityManager(string $name): EntityManager
105
    {
106 4
        return $this->managerRegistry->getManager($name);
107
    }
108
109
    /**
110
     * @param string $name
111
     * @return Connection
112
     */
113 4
    protected function getConnection(string $name): Connection
114
    {
115 4
        return $this->getEntityManager($name)->getConnection();
116
    }
117
}
118