Completed
Push — master ( 53ff75...252438 )
by Markus
04:10
created

ImportBuilder::build()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.25

Importance

Changes 14
Bugs 1 Features 0
Metric Value
c 14
b 1
f 0
dl 0
loc 25
ccs 9
cts 12
cp 0.75
rs 8.5806
cc 4
eloc 12
nc 5
nop 3
crap 4.25
1
<?php
2
namespace Mathielen\ImportEngine\Import;
3
4
use Mathielen\ImportEngine\Event\ImportRequestEvent;
5
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
6
use Mathielen\ImportEngine\Importer\Importer;
7
use Mathielen\ImportEngine\Importer\ImporterRepository;
8
use Mathielen\ImportEngine\ValueObject\ImportConfiguration;
9
use Mathielen\ImportEngine\Storage\StorageInterface;
10
use Mathielen\ImportEngine\Storage\StorageLocator;
11
use Mathielen\ImportEngine\Event\ImportConfigureEvent;
12
use Mathielen\ImportEngine\ValueObject\ImportRequest;
13
use Mathielen\ImportEngine\ValueObject\ImportRun;
14
use Mathielen\ImportEngine\ValueObject\StorageSelection;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
class ImportBuilder
18
{
19
20
    /**
21
     * @var ImporterRepository
22
     */
23
    private $importerRepository;
24
25
    /**
26
     * @var StorageLocator
27
     */
28
    private $storageLocator;
29
30
    /**
31
     * @var EventDispatcherInterface
32
     */
33
    private $eventDispatcher;
34
35 1
    public function __construct(
36
        ImporterRepository $importerRepository,
37
        StorageLocator $storageLocator,
38
        EventDispatcherInterface $eventDispatcher = null)
39
    {
40 1
        $this->importerRepository = $importerRepository;
41 1
        $this->storageLocator = $storageLocator;
42 1
        $this->eventDispatcher = $eventDispatcher;
43 1
    }
44
45
    /**
46
     * @return null|string
47
     */
48
    public function findImporterForStorage(StorageInterface $storage)
49
    {
50
        return $this->importerRepository->find($storage);
51
    }
52
53
    /**
54
     * @return Import
55
     */
56
    public function rebuild(ImportRun $importRun)
57
    {
58
        $importerId = $importRun->getConfiguration()->getImporterId();
59
60
        $importer = $this->importerRepository->get($importerId);
61
        if ($importer->getSourceStorage()) {
62
            $sourceStorage = $importer->getSourceStorage();
63
        } else {
64
            $sourceStorage = $this->storageLocator->getStorage($importRun->getConfiguration()->getSourceStorageSelection());
65
        }
66
67
        return $this->factorImport($importer, $sourceStorage, $importRun);
68
    }
69
70
    /**
71
     * @return Import
72
     * @throws InvalidConfigurationException
73
     */
74 1
    public function buildFromRequest(ImportRequest $importRequest)
75
    {
76 1
        $importerId = $importRequest->getImporterId();
77 1
        $storageSelection = null;
78 1
        $sourceStorage = null;
79
80 1
        if ($importRequest->hasSource()) {
81 1
            $storageSelection = $this->storageLocator->selectStorage(
82 1
                $importRequest->getSourceProviderId(),
83 1
                $importRequest->getSourceId()
84
            );
85
86 1
            $sourceStorage = $this->storageLocator->getStorage($storageSelection);
87
88 1
            if (!$importRequest->hasImporterId()) {
89
                $importerId = $this->findImporterForStorage($sourceStorage);
90
                if (!$importerId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $importerId of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
91
                    throw new InvalidConfigurationException("No importerId was given and there is no importer that matches the storage.");
92
                }
93
94
                $importRequest->setImporterId($importerId);
95
                $this->eventDispatcher->dispatch(
96
                    ImportRequestEvent::DISCOVERED,
97
                    new ImportRequestEvent($importRequest));
98
            }
99
        }
100
101 1
        $importConfiguration = new ImportConfiguration($storageSelection, $importerId);
102
103 1
        return $this->build($importConfiguration, $sourceStorage, $importRequest->getCreatedBy());
104
    }
105
106
    /**
107
     * @return Import
108
     * @throws InvalidConfigurationException
109
     */
110 1
    public function build(ImportConfiguration $importConfiguration, StorageInterface $sourceStorage = null, $createdBy = null)
111
    {
112 1
        $importer = $this->importerRepository->get($importConfiguration->getImporterId());
113 1
        if ($importer->getSourceStorage()) {
114
            $sourceStorage = $importer->getSourceStorage();
115 1
        } elseif (!$sourceStorage) {
116
            throw new InvalidConfigurationException("Either the importRequest or the importer '".$importConfiguration->getImporterId()."' must have a source storage set.");
117
        }
118
119 1
        $importRun = $importConfiguration->toRun($createdBy);
120 1
121
        //apply static context from importer
122
        if (!is_null($importer->getContext())) {
123 1
            $importRun->setContext($importer->getContext());
124
        }
125
126
        $import = $this->factorImport($importer, $sourceStorage, $importRun);
127 1
128
        //after everthing was build, apply softdata from sourcestorage to importrun
129
        //dont do this any earlier, as there might be AFTER_BUILD hooks, that may change
130
        //the sourcestorage configuration
131
        $importRun->setInfo((array) $sourceStorage->info());
132
133 1
        return $import;
134
    }
135 1
136
    /**
137
     * @return Import
138 1
     */
139
    private function factorImport(Importer $importer, StorageInterface $sourceStorage, ImportRun $importRun)
140
    {
141
        $import = Import::build($importer, $sourceStorage, $importRun);
142
143
        //notify system
144
        if ($this->eventDispatcher) {
145
            $this->eventDispatcher->dispatch(
146
                ImportConfigureEvent::AFTER_BUILD,
147
                new ImportConfigureEvent($import));
148 1
149
            $this->eventDispatcher->dispatch(
150
                ImportConfigureEvent::AFTER_BUILD . '.' . $importRun->getConfiguration()->getImporterId(),
151
                new ImportConfigureEvent($import));
152
        }
153
154
        return $import;
155
    }
156
157
}
158