forked from plentymarkets/plentymarkets-shopware-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlentyConnector.php
449 lines (384 loc) · 13.2 KB
/
PlentyConnector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
namespace PlentyConnector;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use PlentyConnector\Connector\BacklogService\Model\Backlog;
use PlentyConnector\Connector\ConfigService\Model\Config;
use PlentyConnector\Connector\IdentityService\IdentityServiceInterface;
use PlentyConnector\Connector\IdentityService\Model\Identity;
use PlentyConnector\Connector\TransferObject\Category\Category;
use PlentyConnector\Connector\TransferObject\PaymentStatus\PaymentStatus;
use PlentyConnector\DependencyInjection\CompilerPass\CleanupDefinitionCompilerPass;
use PlentyConnector\DependencyInjection\CompilerPass\CommandGeneratorCompilerPass;
use PlentyConnector\DependencyInjection\CompilerPass\CommandHandlerCompilerPass;
use PlentyConnector\DependencyInjection\CompilerPass\ConnectorDefinitionCompilerPass;
use PlentyConnector\DependencyInjection\CompilerPass\MappingDefinitionCompilerPass;
use PlentyConnector\DependencyInjection\CompilerPass\QueryGeneratorCompilerPass;
use PlentyConnector\DependencyInjection\CompilerPass\QueryHandlerCompilerPass;
use PlentyConnector\DependencyInjection\CompilerPass\ValidatorServiceCompilerPass;
use PlentyConnector\Installer\CronjobInstaller;
use PlentyConnector\Installer\DatabaseInstaller;
use PlentyConnector\Installer\PermissionInstaller;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
use Shopware_Components_Acl;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
require __DIR__ . '/autoload.php';
/**
* Class PlentyConnector.
*/
class PlentyConnector extends Plugin
{
const PERMISSION_READ = 'read';
const PERMISSION_WRITE = 'write';
/**
* List of all cronjobs
*/
const CRONJOB_SYNCHRONIZE = 'Synchronize';
const CRONJOB_SYNCHRONIZE_ORDERS = 'SynchronizeOrders';
const CRONJOB_CLEANUP = 'Cleanup';
const CRONJOB_BACKLOG = 'ProcessBacklog';
/**
* List of all permissions
*/
public static $permissions = [
self::PERMISSION_READ,
self::PERMISSION_WRITE,
];
/**
* List of all models
*/
public static $models = [
Config::class,
Identity::class,
Backlog::class,
];
/**
* List of all cronjobs
*/
public static $cronjobs = [
self::CRONJOB_SYNCHRONIZE => 60,
self::CRONJOB_BACKLOG => 60,
self::CRONJOB_CLEANUP => 86400,
];
/**
* @param ContainerBuilder $container
*/
public function build(ContainerBuilder $container)
{
$container->setParameter('plenty_connector.plugin_dir', $this->getPath());
$this->loadFile($container, __DIR__ . '/DependencyInjection/services.xml');
$this->loadFile($container, __DIR__ . '/Adapter/ShopwareAdapter/DependencyInjection/services.xml');
$this->loadFile($container, __DIR__ . '/Adapter/PlentymarketsAdapter/DependencyInjection/services.xml');
$this->loadFile($container, __DIR__ . '/Components/Sepa/DependencyInjection/services.xml');
if ($this->pluginExists($container, ['SwagPaymentPaypal', 'SwagPaymentPayPalInstallments', 'SwagPaymentPaypalPlus'])) {
$this->loadFile($container, __DIR__ . '/Components/PayPal/DependencyInjection/services.xml');
}
if ($this->pluginExists($container, ['SwagBundle'])) {
$this->loadFile($container, __DIR__ . '/Components/Bundle/DependencyInjection/services.xml');
}
$container->addCompilerPass(new CleanupDefinitionCompilerPass());
$container->addCompilerPass(new CommandGeneratorCompilerPass());
$container->addCompilerPass(new CommandHandlerCompilerPass());
$container->addCompilerPass(new ConnectorDefinitionCompilerPass());
$container->addCompilerPass(new MappingDefinitionCompilerPass());
$container->addCompilerPass(new QueryGeneratorCompilerPass());
$container->addCompilerPass(new QueryHandlerCompilerPass());
$container->addCompilerPass(new ValidatorServiceCompilerPass());
parent::build($container);
}
/**
* @param InstallContext $context
*/
public function install(InstallContext $context)
{
$this->clearOldDatabaseTables();
/**
* @var ModelManager $models
*/
$models = $this->container->get('models');
/**
* @var Shopware_Components_Acl $acl
*/
$acl = $this->container->get('acl');
// Models
$databaseInstaller = new DatabaseInstaller(
$models,
self::$models
);
$databaseInstaller->install($context);
// Cronjobs
$cronjobInstaller = new CronjobInstaller(
$models->getConnection(),
self::$cronjobs
);
$cronjobInstaller->install($context);
// Permissions
$permissionInstaller = new PermissionInstaller(
$models,
$acl,
self::$permissions
);
$permissionInstaller->install($context);
parent::install($context);
}
/**
* @param UpdateContext $context
*/
public function update(UpdateContext $context)
{
if ($this->updateNeeded($context, '2.0.0')) {
$this->clearOldDatabaseTables();
}
/**
* @var ModelManager $models
*/
$models = $this->container->get('models');
/**
* @var Shopware_Components_Acl $acl
*/
$acl = $this->container->get('acl');
// Models
$databaseInstaller = new DatabaseInstaller(
$models,
self::$models
);
$databaseInstaller->update($context);
// Cronjobs
$cronjobInstaller = new CronjobInstaller(
$models->getConnection(),
self::$cronjobs
);
$cronjobInstaller->update($context);
// Permissions
$permissionInstaller = new PermissionInstaller(
$models,
$acl,
self::$permissions
);
$permissionInstaller->update($context);
if ($this->updateNeeded($context, '2.0.0-rc2') && $this->updatePossible($context, '2.0.0')) {
$this->clearCategoryIdentities();
$this->clearPaymentStatusIdentities();
$this->clearLastChangedConfigEntries();
}
if ($this->updateNeeded($context, '4.0.0') && $this->updatePossible($context, '2.0.0')) {
$this->clearLastChangedConfigEntries();
}
if ($this->updateNeeded($context, '4.0.4') && $this->updatePossible($context, '2.0.0')) {
$this->updateBacklogTable();
}
parent::update($context);
}
/**
* @param UninstallContext $context
*/
public function uninstall(UninstallContext $context)
{
/**
* @var ModelManager $models
*/
$models = $this->container->get('models');
/**
* @var Shopware_Components_Acl $acl
*/
$acl = $this->container->get('acl');
// Models
$databaseInstaller = new DatabaseInstaller(
$models,
self::$models
);
$databaseInstaller->uninstall($context);
// Cronjobs
$cronjobInstaller = new CronjobInstaller(
$models->getConnection(),
self::$cronjobs
);
$cronjobInstaller->uninstall($context);
// Permissions
$permissionInstaller = new PermissionInstaller(
$models,
$acl,
self::$permissions
);
$permissionInstaller->uninstall($context);
parent::uninstall($context);
}
/**
* @param ActivateContext $context
*
* @throws \RuntimeException
*/
public function activate(ActivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
parent::activate($context);
}
/**
* @param ContainerBuilder $container
* @param array $plugins
*
* @return bool
*/
private function pluginExists(ContainerBuilder $container, array $plugins)
{
$folders = $container->getParameter('shopware.plugin_directories');
foreach ($plugins as $pluginName) {
foreach ($folders as $folder) {
if (file_exists($folder . 'Backend/' . $pluginName)) {
return true;
}
if (file_exists($folder . 'Core/' . $pluginName)) {
return true;
}
if (file_exists($folder . 'Frontend/' . $pluginName)) {
return true;
}
}
}
return false;
}
/**
* @param UpdateContext $context
* @param $targetVersion
*
* @return mixed
*/
private function updateNeeded(UpdateContext $context, $targetVersion)
{
return version_compare($context->getCurrentVersion(), $targetVersion, '<');
}
/**
* @param UpdateContext $context
* @param $targetVersion
*
* @return mixed
*/
private function updatePossible(UpdateContext $context, $targetVersion)
{
return version_compare($context->getCurrentVersion(), $targetVersion, '>');
}
/**
* remove category identities as we changed the mapping format.
*/
private function clearCategoryIdentities()
{
/**
* @var IdentityServiceInterface $identityService
*/
$identityService = $this->container->get('plenty_connector.identity_service');
foreach ($identityService->findBy(['objectType' => Category::TYPE]) as $identity) {
$identityService->remove($identity);
}
}
/**
* remove category identities as we changed what informations are mapped
*/
private function clearPaymentStatusIdentities()
{
/**
* @var IdentityServiceInterface $identityService
*/
$identityService = $this->container->get('plenty_connector.identity_service');
foreach ($identityService->findBy(['objectType' => PaymentStatus::TYPE]) as $identity) {
$identityService->remove($identity);
}
}
/**
* reimport everything as we changed the config format
*/
private function clearLastChangedConfigEntries()
{
/**
* @var EntityManagerInterface $entityManager
*/
$entityManager = $this->container->get('models');
$repository = $entityManager->getRepository(Config::class);
/**
* @var Config $element
*/
foreach ($repository->findAll() as $element) {
if (false === stripos($element->getName(), 'LastChangeDateTime')) {
continue;
}
$entityManager->remove($element);
}
$entityManager->flush();
}
private function updateBacklogTable()
{
/**
* @var Connection $connection
*/
$connection = $this->container->get('dbal_connection');
$query = 'UPDATE plenty_backlog SET status = :statusNew WHERE status = :statusOld';
$connection->executeQuery($query, [
':statusNew' => Backlog::STATUS_OPEN,
':statusOld' => '',
]);
}
private function clearOldDatabaseTables()
{
$tables = [
'plenty_log',
'plenty_config',
'plenty_mapping_attribute_group',
'plenty_mapping_attribute_option',
'plenty_mapping_category',
'plenty_mapping_category_old',
'plenty_mapping_country',
'plenty_mapping_currency',
'plenty_mapping_customer',
'plenty_mapping_customer_billing_address',
'plenty_mapping_customer_class',
'plenty_mapping_item',
'plenty_mapping_item_bundle',
'plenty_mapping_item_variant',
'plenty_mapping_measure_unit',
'plenty_mapping_method_of_payment',
'plenty_mapping_order_status',
'plenty_mapping_payment_status',
'plenty_mapping_producer',
'plenty_mapping_property',
'plenty_mapping_property_group',
'plenty_mapping_referrer',
'plenty_mapping_shipping_profile',
'plenty_mapping_shop',
'plenty_mapping_vat',
];
/**
* @var Connection $connection
*/
$connection = $this->container->get('dbal_connection');
foreach ($tables as $table) {
try {
$query = 'DROP TABLE IF EXISTS ?';
$connection->query($query, [$table]);
} catch (Exception $exception) {
// fail silently
}
}
}
/**
* @param ContainerBuilder $container
* @param $filename
*/
private function loadFile(ContainerBuilder $container, $filename)
{
if (!is_file($filename)) {
return;
}
$loader = new XmlFileLoader(
$container,
new FileLocator()
);
$loader->load($filename);
}
}