Completed
Push — master ( 56ccae...2c3aff )
by Markus
04:24
created

DoctrineEndpoint::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mathielen\ImportEngineBundle\Endpoint;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Mathielen\DataImport\Event\ImportProcessEvent;
7
8
class DoctrineEndpoint
9
{
10
    /**
11
     * @var ObjectManager
12
     */
13
    private $objectManager;
14
15
    private $chunkSize = null;
16
    private $currentChunkCount = 0;
17
18
    public function __construct(ObjectManager $objectManager, $chunkSize = null)
19
    {
20
        $this->objectManager = $objectManager;
21
        $this->chunkSize = $chunkSize;
22
    }
23
24
    public function add($entity)
25
    {
26
        $this->objectManager->persist($entity);
27
        ++$this->currentChunkCount;
28
29
        if ($this->chunkCompleted()) {
30
            $this->objectManager->flush();
31
            $this->currentChunkCount = 0;
32
        }
33
    }
34
35
    protected function chunkCompleted()
36
    {
37
        return $this->chunkSize && $this->currentChunkCount >= $this->chunkSize;
38
    }
39
40
    public function prepare(ImportProcessEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $this->added = 0;
0 ignored issues
show
Bug introduced by
The property added does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
    }
44
45
    public function finish(ImportProcessEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        $this->objectManager->flush();
48
    }
49
}
50