Completed
Push — master ( 378c5b...5052d2 )
by Markus
08:56
created

ImportRunner   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 109
Duplicated Lines 5.5 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 88.37%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 11
dl 6
loc 109
ccs 38
cts 43
cp 0.8837
rs 10
c 0
b 0
f 0

6 Methods

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