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

DoctrineEndpoint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 2
cbo 0
dl 0
loc 42
ccs 0
cts 26
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 10 2
A chunkCompleted() 0 4 2
A prepare() 0 4 1
A finish() 0 4 1
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