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
|
1 |
|
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
|
1 |
|
} 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
|
1 |
|
); |
85
|
|
|
|
86
|
1 |
|
$sourceStorage = $this->storageLocator->getStorage($storageSelection); |
87
|
|
|
|
88
|
1 |
|
if (!$importRequest->hasImporterId()) { |
89
|
|
|
$importerId = $this->findImporterForStorage($sourceStorage); |
90
|
|
|
if (!$importerId) { |
|
|
|
|
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
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
$importConfiguration = new ImportConfiguration($storageSelection, $importerId); |
102
|
|
|
|
103
|
1 |
|
return $this->build($importConfiguration, $sourceStorage, $importRequest->getCreatedBy(), $importRequest->getContext()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Import |
108
|
|
|
* @throws InvalidConfigurationException |
109
|
|
|
*/ |
110
|
1 |
|
public function build(ImportConfiguration $importConfiguration, StorageInterface $sourceStorage = null, $createdBy = null, $requestContext = 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
|
|
|
|
121
|
|
|
//apply static context from importer & request |
122
|
1 |
|
$context = $requestContext; |
123
|
1 |
|
if (!is_null($importer->getContext()) && !empty($importer->getContext())) { |
124
|
|
|
$importerContext = $importer->getContext(); |
125
|
|
|
if ($importerContext && $requestContext) { |
126
|
|
|
$context = array_merge($importerContext, $requestContext); |
127
|
|
|
} else { |
128
|
|
|
$context = $importerContext; |
129
|
|
|
} |
130
|
|
|
} |
131
|
1 |
|
$importRun->setContext($context); |
132
|
|
|
|
133
|
1 |
|
$import = $this->factorImport($importer, $sourceStorage, $importRun); |
134
|
|
|
|
135
|
|
|
//after everthing was build, apply softdata from sourcestorage to importrun |
136
|
|
|
//dont do this any earlier, as there might be AFTER_BUILD hooks, that may change |
137
|
|
|
//the sourcestorage configuration |
138
|
1 |
|
$importRun->setInfo((array) $sourceStorage->info()); |
139
|
|
|
|
140
|
1 |
|
return $import; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return Import |
145
|
|
|
*/ |
146
|
1 |
|
private function factorImport(Importer $importer, StorageInterface $sourceStorage, ImportRun $importRun) |
147
|
|
|
{ |
148
|
1 |
|
$import = Import::build($importer, $sourceStorage, $importRun); |
149
|
|
|
|
150
|
|
|
//notify system |
151
|
1 |
|
if ($this->eventDispatcher) { |
152
|
|
|
$this->eventDispatcher->dispatch( |
153
|
|
|
ImportConfigureEvent::AFTER_BUILD, |
154
|
|
|
new ImportConfigureEvent($import)); |
155
|
|
|
|
156
|
|
|
$this->eventDispatcher->dispatch( |
157
|
|
|
ImportConfigureEvent::AFTER_BUILD . '.' . $importRun->getConfiguration()->getImporterId(), |
158
|
|
|
new ImportConfigureEvent($import)); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $import; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: