Test Failed
Push — master ( bf1804...12692f )
by Hirofumi
03:59
created

ManagerRegistryAwareTrait::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Shippinno\Job\Infrastructure\Persistence\Doctrine;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\EntityManager;
7
8
trait ManagerRegistryAwareTrait
9
{
10
    /**
11
     * @var ManagerRegistry|null $managerRegistry
12
     */
13
    protected $managerRegistry;
14
15
    /**
16
     * @param ManagerRegistry|null $managerRegistry
17
     */
18 2
    public function setManagerRegistry(?ManagerRegistry $managerRegistry)
19
    {
20 2
        $this->managerRegistry = $managerRegistry;
21 2
    }
22
23
    /**
24
     * @return void
25
     */
26
    public function flush(): void
27
    {
28
        if ($this->hasEntityManager()) {
29
            /** @var EntityManager $entityManager */
30
            $entityManager =  $this->managerRegistry->getManager();
31
            $connection = $entityManager->getConnection();
32
            if (!$connection->ping()) {
33
                $connection->close();
34
                $connection->connect();
35
            }
36
            $entityManager->flush();
37
        }
38
    }
39
40
    /**
41
     * @return void
42
     */
43
    public function clear(): void
44
    {
45
        if ($this->hasEntityManager()) {
46
            $this->managerRegistry->getManager()->clear();
47
        }
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    protected function hasEntityManager(): bool
54
    {
55
        return null !== $this->managerRegistry;
56
    }
57
}
58