Passed
Push — master ( 99df08...45269c )
by Hirofumi
02:05
created

ManagerRegistryAwareTrait::setManagerRegistry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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