This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeidelPayment.php
150 lines (123 loc) · 5.76 KB
/
HeidelPayment.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
<?php
declare(strict_types=1);
namespace HeidelPayment;
use HeidelPayment\Components\DependencyInjection\CompilerPass\PaymentStatusMapperCompilerPass;
use HeidelPayment\Components\DependencyInjection\CompilerPass\ViewBehaviorCompilerPass;
use HeidelPayment\Components\DependencyInjection\CompilerPass\WebhookCompilerPass;
use HeidelPayment\Installers\Attributes;
use HeidelPayment\Installers\Database;
use HeidelPayment\Installers\Document;
use HeidelPayment\Installers\PaymentMethods;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
use Symfony\Component\DependencyInjection\ContainerBuilder;
//Load the heidelpay-php SDK
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
class HeidelPayment extends Plugin
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new WebhookCompilerPass());
$container->addCompilerPass(new ViewBehaviorCompilerPass());
$container->addCompilerPass(new PaymentStatusMapperCompilerPass());
parent::build($container);
}
/**
* {@inheritdoc}
*/
public function install(InstallContext $context): void
{
$this->applyUpdates(null, $context->getCurrentVersion());
parent::install($context);
}
/**
* {@inheritdoc}
*/
public function uninstall(UninstallContext $context): void
{
$snippetNamespace = $this->container->get('snippets')->getNamespace('backend/heidel_payment/pluginmanager');
if (!$context->keepUserData()) {
(new Database($this->container->get('dbal_connection')))->uninstall();
(new Document($this->container->get('dbal_connection'), $this->container->get('translation')))->uninstall();
(new Attributes($this->container->get('shopware_attribute.crud_service'), $this->container->get('models')))->uninstall();
}
(new PaymentMethods($this->container->get('models'), $this->container->get('shopware_attribute.data_persister')))->uninstall();
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
$context->scheduleMessage($snippetNamespace->get('uninstall/message'));
}
/**
* {@inheritdoc}
*/
public function update(UpdateContext $context): void
{
$snippetNamespace = $this->container->get('snippets')->getNamespace('backend/heidel_payment/pluginmanager');
$this->applyUpdates($context->getCurrentVersion(), $context->getUpdateVersion());
$context->scheduleMessage($snippetNamespace->get('update/message'));
parent::update($context);
}
public function activate(ActivateContext $context): void
{
$snippetNamespace = $this->container->get('snippets')->getNamespace('backend/heidel_payment/pluginmanager');
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
$context->scheduleMessage($snippetNamespace->get('activate/message'));
}
public function deactivate(DeactivateContext $context): void
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
}
public function getVersion(): string
{
return $this->container->get('dbal_connection')->createQueryBuilder()
->select('version')
->from('s_core_plugins')
->where('name = :name')
->setParameter('name', $this->getName())
->execute()->fetchColumn();
}
private function applyUpdates(?string $oldVersion = null, ?string $newVersion = null): bool
{
$versionClosures = [
'1.0.0' => function () {
$modelManager = $this->container->get('models');
$connection = $this->container->get('dbal_connection');
$crudService = $this->container->get('shopware_attribute.crud_service');
$translation = $this->container->get('translation');
$dataPersister = $this->container->get('shopware_attribute.data_persister');
(new Document($connection, $translation))->install();
(new Database($connection))->install();
(new Attributes($crudService, $modelManager))->install();
(new PaymentMethods($modelManager, $dataPersister))->install();
return true;
},
'1.0.2' => function () use ($oldVersion, $newVersion): void {
$modelManager = $this->container->get('models');
$dataPersister = $this->container->get('shopware_attribute.data_persister');
(new PaymentMethods($modelManager, $dataPersister))->update($oldVersion ?? '', $newVersion ?? '');
},
'1.2.0' => function () use ($oldVersion, $newVersion): void {
$modelManager = $this->container->get('models');
$crudService = $this->container->get('shopware_attribute.crud_service');
$dataPersister = $this->container->get('shopware_attribute.data_persister');
(new Attributes($crudService, $modelManager))->install();
(new PaymentMethods($modelManager, $dataPersister))->update($oldVersion ?? '', $newVersion ?? '');
},
];
foreach ($versionClosures as $version => $versionClosure) {
if ($oldVersion === null || (version_compare($oldVersion, $version, '<') && version_compare($version, $newVersion, '<='))) {
if (!$versionClosure($this)) {
return false;
}
}
}
return true;
}
}