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

ImportRunner::preview()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 21
ccs 9
cts 11
cp 0.8182
rs 9.3142
cc 3
eloc 12
nc 3
nop 2
crap 3.054
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