Completed
Push — master ( 0fb3d7...53ff75 )
by Markus
03:02
created

ImportRunner   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 21.78 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 87.5%

Importance

Changes 11
Bugs 3 Features 0
Metric Value
wmc 15
c 11
b 3
f 0
lcom 1
cbo 8
dl 22
loc 101
ccs 35
cts 40
cp 0.875
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 4 1
A dryRun() 8 8 1
A run() 8 8 1
A __construct() 0 9 2
B process() 6 19 7
A preview() 0 21 3

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
namespace Mathielen\ImportEngine\Import\Run;
3
4
use Ddeboer\DataImport\Workflow;
5
use Mathielen\DataImport\Event\ImportProcessEvent;
6
use Mathielen\ImportEngine\Import\Import;
7
use Mathielen\ImportEngine\Import\Workflow\DefaultWorkflowFactory;
8
use Mathielen\ImportEngine\Import\Workflow\WorkflowFactoryInterface;
9
use Mathielen\ImportEngine\Exception\ImportRunException;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
class ImportRunner
13
{
14
15
    /**
16
     * @var WorkflowFactoryInterface
17
     */
18
    private $workflowFactory;
19
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    private $eventDispatcher;
24
25 14
    public function __construct(WorkflowFactoryInterface $workflowFactory=null, EventDispatcherInterface $eventDispatcher=null)
26
    {
27 14
        if (!$workflowFactory) {
28 8
            $workflowFactory = new DefaultWorkflowFactory();
29
        }
30
31 14
        $this->workflowFactory = $workflowFactory;
32 14
        $this->eventDispatcher = $eventDispatcher;
33 14
    }
34
35
    /**
36
     * @return ImportRunner
37
     */
38 2
    public static function build(WorkflowFactoryInterface $workflowFactory=null)
39
    {
40 2
        return new self($workflowFactory);
41
    }
42
43 14
    private function process(Workflow $workflow, Import $import)
44
    {
45 14
        $e = null;
46 14
        $importRun = null;
47
48 14
        if ($this->eventDispatcher && $importRun = $import->getRun()) {
49
            $e = new ImportProcessEvent($import);
50
        }
51
52 14 View Code Duplication
        if ($e && $importRun->getConfiguration()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            $this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_PREPARE.'.'.$importRun->getConfiguration()->getImporterId(), $e);
54
        }
55
56 14
        $workflow->process();
57
58 14 View Code Duplication
        if ($e && $importRun->getConfiguration()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
59
            $this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_FINISH.'.'.$importRun->getConfiguration()->getImporterId(), $e);
60
        }
61 14
    }
62
63
    /**
64
     * @return array
65
     */
66 2
    public function preview(Import $import, $offset = 0)
67
    {
68 2
        $importRun = $import->getRun();
69 2
        $previewResult = array('from'=>array(), 'to'=>array());
70
71 2
        $workflow = $this->workflowFactory->buildPreviewWorkflow($import, $previewResult, $offset);
72 2
        $this->process($workflow, $import);
73
74 2
        if (0 == count($previewResult['from'])) {
75
            throw new ImportRunException("Unable to preview row with offset '$offset'. EOF?", $importRun);
76
        }
77
78
        //cleanup from writer
79 2
        if (count($previewResult['to']) > 0) {
80 2
            $previewResult['to'] = $previewResult['to'][0];
81
        } else {
82
            $previewResult['to'] = array_fill_keys($import->mappings()->getTargetFields(), null);
83
        }
84
85 2
        return $previewResult;
86
    }
87
88
    /**
89
     * @return Import
90
     */
91 3 View Code Duplication
    public function dryRun(Import $import)
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...
92
    {
93 3
        $importRun = $import->getRun();
94 3
        $workflow = $this->workflowFactory->buildDryrunWorkflow($import, $importRun);
95 3
        $this->process($workflow, $import);
96
97 3
        return $importRun;
98
    }
99
100
    /**
101
     * @return Import
102
     */
103 11 View Code Duplication
    public function run(Import $import)
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...
104
    {
105 11
        $importRun = $import->getRun();
106 11
        $workflow = $this->workflowFactory->buildRunWorkflow($import, $importRun);
107 11
        $this->process($workflow, $import);
108
109 11
        return $importRun;
110
    }
111
112
}
113