ImportRunEventSubscriber   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 31.37 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 75.51%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 32
loc 102
ccs 37
cts 49
cp 0.7551
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getSubscribedEvents() 0 13 1
A onImportPrepare() 0 4 1
A onImportFinish() 0 9 2
A onAfterRead() 0 6 1
A onAfterFilter() 8 8 2
A onAfterConversion() 8 8 2
A onAfterConversionFilter() 8 8 2
A onAfterValidate() 8 8 2
A onAfterWrite() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mathielen\ImportEngine\Import\Run;
4
5
use Mathielen\DataImport\Event\ImportProcessEvent;
6
use Mathielen\ImportEngine\Import\Import;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Mathielen\DataImport\Event\ImportItemEvent;
10
11
class ImportRunEventSubscriber implements EventSubscriberInterface
12
{
13
    /**
14
     * @var Import
15
     */
16
    private $import;
17
18
    private $statistics;
19
20
    private $isDryRun;
21
22 13
    public function __construct(Import $import, $isDryRun = false)
23
    {
24 13
        $this->isDryRun = $isDryRun;
25 13
        $this->import = $import;
26 13
        $this->statistics = array(
27
            'processed' => 0,
28
            'written' => 0,
29
            'skipped' => 0,
30
            'invalid' => 0,
31
        );
32 13
    }
33
34 13
    public static function getSubscribedEvents()
35
    {
36
        return array(
37 13
            ImportProcessEvent::AFTER_PREPARE => array('onImportPrepare', 999999),
38 13
            ImportItemEvent::AFTER_READ => array('onAfterRead', 0),
39 13
            ImportItemEvent::AFTER_FILTER => array('onAfterFilter', 0),
40 13
            ImportItemEvent::AFTER_CONVERSION => array('onAfterConversion', 0),
41 13
            ImportItemEvent::AFTER_CONVERSIONFILTER => array('onAfterConversionFilter', 0),
42 13
            ImportItemEvent::AFTER_VALIDATION => array('onAfterValidate', 0),
43 13
            ImportItemEvent::AFTER_WRITE => array('onAfterWrite', 0),
44
            ImportProcessEvent::AFTER_FINISH => array('onImportFinish', 999999),
45
        );
46
    }
47
48 13
    public function onImportPrepare(ImportProcessEvent $event)
49
    {
50 13
        $event->setContext($this->import);
51 13
    }
52
53 13
    public function onImportFinish(ImportProcessEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
54
    {
55 13
        if (!$this->isDryRun) {
56 11
            $this->import->getRun()->finish();
57
        }
58
59
        //remove the subscriber when its done
60 13
        $eventDispatcher->removeSubscriber($this);
61 13
    }
62
63 13
    public function onAfterRead(ImportItemEvent $event)
64
    {
65 13
        ++$this->statistics['processed'];
66
67 13
        $this->import->getRun()->setStatistics($this->statistics);
68 13
    }
69
70 13 View Code Duplication
    public function onAfterFilter(ImportItemEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 13
        if (!$event->getCurrentResult()) {
73 1
            ++$this->statistics['skipped'];
74
75 1
            $this->import->getRun()->setStatistics($this->statistics);
76
        }
77 13
    }
78
79 View Code Duplication
    public function onAfterConversion(ImportItemEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        if (!$event->getCurrentResult()) {
82
            ++$this->statistics['skipped'];
83
84
            $this->import->getRun()->setStatistics($this->statistics);
85
        }
86
    }
87
88 12 View Code Duplication
    public function onAfterConversionFilter(ImportItemEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 12
        if (!$event->getCurrentResult()) {
91
            ++$this->statistics['skipped'];
92
93
            $this->import->getRun()->setStatistics($this->statistics);
94
        }
95 12
    }
96
97 View Code Duplication
    public function onAfterValidate(ImportItemEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        if (!$event->getCurrentResult()) {
100
            ++$this->statistics['invalid'];
101
102
            $this->import->getRun()->setStatistics($this->statistics);
103
        }
104
    }
105
106 12
    public function onAfterWrite(ImportItemEvent $event)
107
    {
108 12
        ++$this->statistics['written'];
109
110 12
        $this->import->getRun()->setStatistics($this->statistics);
111 12
    }
112
}
113