1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mathielen\ImportEngine\Import; |
4
|
|
|
|
5
|
|
|
use Mathielen\ImportEngine\Event\ImportRequestEvent; |
6
|
|
|
use Mathielen\ImportEngine\Exception\InvalidConfigurationException; |
7
|
|
|
use Mathielen\ImportEngine\Importer\Importer; |
8
|
|
|
use Mathielen\ImportEngine\Importer\ImporterRepository; |
9
|
|
|
use Mathielen\ImportEngine\ValueObject\ImportConfiguration; |
10
|
|
|
use Mathielen\ImportEngine\Storage\StorageInterface; |
11
|
|
|
use Mathielen\ImportEngine\Storage\StorageLocator; |
12
|
|
|
use Mathielen\ImportEngine\Event\ImportConfigureEvent; |
13
|
|
|
use Mathielen\ImportEngine\ValueObject\ImportRequest; |
14
|
|
|
use Mathielen\ImportEngine\ValueObject\ImportRun; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
|
17
|
|
|
class ImportBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ImporterRepository |
21
|
|
|
*/ |
22
|
|
|
private $importerRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var StorageLocator |
26
|
|
|
*/ |
27
|
|
|
private $storageLocator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EventDispatcherInterface |
31
|
|
|
*/ |
32
|
|
|
private $eventDispatcher; |
33
|
|
|
|
34
|
1 |
|
public function __construct( |
35
|
|
|
ImporterRepository $importerRepository, |
36
|
|
|
StorageLocator $storageLocator, |
37
|
|
|
EventDispatcherInterface $eventDispatcher = null) |
38
|
|
|
{ |
39
|
1 |
|
$this->importerRepository = $importerRepository; |
40
|
1 |
|
$this->storageLocator = $storageLocator; |
41
|
1 |
|
$this->eventDispatcher = $eventDispatcher; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return null|string |
46
|
|
|
*/ |
47
|
|
|
public function findImporterForStorage(StorageInterface $storage) |
48
|
|
|
{ |
49
|
|
|
return $this->importerRepository->find($storage); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Import |
54
|
|
|
*/ |
55
|
|
|
public function rebuild(ImportRun $importRun) |
56
|
|
|
{ |
57
|
|
|
$importerId = $importRun->getConfiguration()->getImporterId(); |
58
|
|
|
|
59
|
|
|
$importer = $this->importerRepository->get($importerId); |
60
|
|
|
if ($importer->getSourceStorage()) { |
61
|
|
|
$sourceStorage = $importer->getSourceStorage(); |
62
|
|
|
} else { |
63
|
|
|
$sourceStorage = $this->storageLocator->getStorage($importRun->getConfiguration()->getSourceStorageSelection()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->factorImport($importer, $sourceStorage, $importRun); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Import |
71
|
|
|
* |
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) { |
|
|
|
|
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(), $importRequest->getContext()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Import |
108
|
|
|
* |
109
|
|
|
* @throws InvalidConfigurationException |
110
|
|
|
*/ |
111
|
1 |
|
public function build(ImportConfiguration $importConfiguration, StorageInterface $sourceStorage = null, $createdBy = null, $requestContext = null) |
112
|
|
|
{ |
113
|
1 |
|
$importer = $this->importerRepository->get($importConfiguration->getImporterId()); |
114
|
1 |
|
if ($importer->getSourceStorage()) { |
115
|
|
|
$sourceStorage = $importer->getSourceStorage(); |
116
|
1 |
|
} elseif (!$sourceStorage) { |
117
|
|
|
throw new InvalidConfigurationException("Either the importRequest or the importer '".$importConfiguration->getImporterId()."' must have a source storage set."); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$importRun = $importConfiguration->toRun($createdBy); |
121
|
|
|
|
122
|
|
|
//apply static context from importer & request |
123
|
1 |
|
$context = $requestContext; |
124
|
1 |
|
if (!is_null($importer->getContext()) && !empty($importer->getContext())) { |
125
|
|
|
$importerContext = $importer->getContext(); |
126
|
|
|
if ($importerContext && $requestContext) { |
127
|
|
|
$context = array_merge($importerContext, $requestContext); |
128
|
|
|
} else { |
129
|
|
|
$context = $importerContext; |
130
|
|
|
} |
131
|
|
|
} |
132
|
1 |
|
$importRun->setContext($context); |
133
|
|
|
|
134
|
1 |
|
$import = $this->factorImport($importer, $sourceStorage, $importRun); |
135
|
|
|
|
136
|
|
|
//after everthing was build, apply softdata from sourcestorage to importrun |
137
|
|
|
//dont do this any earlier, as there might be AFTER_BUILD hooks, that may change |
138
|
|
|
//the sourcestorage configuration |
139
|
1 |
|
$importRun->setInfo((array) $sourceStorage->info()); |
140
|
|
|
|
141
|
1 |
|
return $import; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return Import |
146
|
|
|
*/ |
147
|
1 |
|
private function factorImport(Importer $importer, StorageInterface $sourceStorage, ImportRun $importRun) |
148
|
|
|
{ |
149
|
1 |
|
$import = Import::build($importer, $sourceStorage, $importRun); |
150
|
|
|
|
151
|
|
|
//notify system |
152
|
1 |
|
if ($this->eventDispatcher) { |
153
|
|
|
$this->eventDispatcher->dispatch( |
154
|
|
|
ImportConfigureEvent::AFTER_BUILD, |
155
|
|
|
new ImportConfigureEvent($import)); |
|
|
|
|
156
|
|
|
|
157
|
|
|
$this->eventDispatcher->dispatch( |
158
|
|
|
ImportConfigureEvent::AFTER_BUILD.'.'.$importRun->getConfiguration()->getImporterId(), |
|
|
|
|
159
|
|
|
new ImportConfigureEvent($import)); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $import; |
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: