DoctrinePersister   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 52
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A persist() 0 10 2
A finalize() 0 3 1
A clear() 0 13 6
A writeBatch() 0 6 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\Persister;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
9
/**
10
 * @internal
11
 */
12
class DoctrinePersister implements PersisterInterface
13
{
14
    private $em;
15
    private $batch = 0;
16
17
    private $whiteList = [];
18
    private $blackList = [];
19
20 5
    public function __construct(EntityManagerInterface $em)
21
    {
22 5
        $this->em = $em;
23 5
    }
24
25 5
    public function persist($entity, $batchCount = 20, $whiteList = [], $blackList = [])
26
    {
27 5
        ++$this->batch;
28 5
        $this->whiteList = $whiteList;
29 5
        $this->blackList = $blackList;
30
31 5
        $this->em->persist($entity);
32
33 5
        if ($this->batch >= $batchCount) {
34 1
            $this->writeBatch();
35
        }
36 5
    }
37
38 3
    public function finalize()
39
    {
40 3
        $this->writeBatch();
41 2
    }
42
43 4
    private function writeBatch()
44
    {
45 4
        $this->em->flush();
46 4
        $this->clear();
47
48 3
        $this->batch = 0;
49 3
    }
50
51 4
    private function clear()
52
    {
53 4
        if (empty($this->whiteList) && empty($this->blackList)) {
54 2
            $this->em->clear();
55 2
        } elseif (!empty($this->whiteList)) {
56 1
            foreach ($this->whiteList as $class) {
57 1
                $this->em->clear($class);
58
            }
59 1
        } elseif (!empty($this->blackList)) {
60 1
            throw new \Exception('unsupported blacklist');
61
        }
62
63 3
        gc_collect_cycles();
64 3
    }
65
}
66