-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathWebhookSystemConfigHelper.php
170 lines (142 loc) · 5.23 KB
/
WebhookSystemConfigHelper.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
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\PayPal\Webhook\Registration;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Swag\PayPal\Setting\Service\SettingsValidationServiceInterface;
use Swag\PayPal\Setting\Settings;
use Swag\PayPal\Webhook\WebhookServiceInterface;
#[Package('checkout')]
class WebhookSystemConfigHelper
{
private const WEBHOOK_KEYS = [
Settings::CLIENT_ID,
Settings::CLIENT_SECRET,
Settings::CLIENT_ID_SANDBOX,
Settings::CLIENT_SECRET_SANDBOX,
Settings::SANDBOX,
Settings::WEBHOOK_ID,
];
private LoggerInterface $logger;
private WebhookServiceInterface $webhookService;
private SystemConfigService $systemConfigService;
private SettingsValidationServiceInterface $settingsValidationService;
/**
* @internal
*/
public function __construct(
LoggerInterface $logger,
WebhookServiceInterface $webhookService,
SystemConfigService $systemConfigService,
SettingsValidationServiceInterface $settingsValidationService,
) {
$this->logger = $logger;
$this->webhookService = $webhookService;
$this->systemConfigService = $systemConfigService;
$this->settingsValidationService = $settingsValidationService;
}
/**
* @param array<string, array<string, mixed>> $newData
*
* @return \Throwable[]
*/
public function checkWebhookBefore(array $newData): array
{
$errors = [];
foreach ($newData as $salesChannelId => $newSettings) {
if (!$salesChannelId || $salesChannelId === 'null') {
$salesChannelId = null;
}
$oldDistinctSettings = $this->fetchSettings($salesChannelId);
if (empty($oldDistinctSettings)) {
// Sales Channel previously had no own configuration
continue;
}
$oldActualSettings = $this->fetchSettings($salesChannelId, true);
if ($this->settingsValidationService->checkForMissingSetting($oldActualSettings) !== null) {
// this sales channel has no valid setting
continue;
}
if (!$this->configHasChangedSettings($newSettings, $oldActualSettings)) {
// No writing of new credentials in this Sales Channel
continue;
}
// since credentials will be changed: try to deregister with old credentials
try {
$this->webhookService->deregisterWebhook($salesChannelId);
} catch (\Throwable $e) {
$errors[] = $e;
$this->logger->error($e->getMessage(), ['error' => $e]);
}
}
return $errors;
}
/**
* @param array<?string> $salesChannelIds
*
* @return \Throwable[]
*/
public function checkWebhookAfter(array $salesChannelIds): array
{
$errors = [];
foreach ($salesChannelIds as $salesChannelId) {
if (!$salesChannelId || $salesChannelId === 'null') {
$salesChannelId = null;
}
$newSettings = $this->fetchSettings($salesChannelId);
if (empty(\array_filter($newSettings))) {
// has no own valid configuration
continue;
}
$newSettings[Settings::SANDBOX] = $this->systemConfigService->get(Settings::SANDBOX, $salesChannelId);
if ($this->settingsValidationService->checkForMissingSetting($newSettings) !== null) {
// this sales channel has no valid setting
continue;
}
try {
$this->webhookService->registerWebhook($salesChannelId);
} catch (\Throwable $e) {
$errors[] = $e;
$this->logger->error($e->getMessage(), ['error' => $e]);
}
}
return $errors;
}
/**
* @param array<string, mixed> $config
*/
public function needsCheck(array $config): bool
{
return !empty($this->filterSettings($config));
}
private function fetchSettings(?string $salesChannelId, bool $inherit = false): array
{
$settings = [];
foreach (self::WEBHOOK_KEYS as $key) {
$value = $this->systemConfigService->get($key, $salesChannelId);
if (!$inherit && $salesChannelId !== null && $value === $this->systemConfigService->get($key)) {
continue;
}
$settings[$key] = $value;
}
return $settings;
}
private function configHasChangedSettings(array $newSettings, array $oldSettings): bool
{
return !empty(\array_diff_assoc($this->filterSettings($newSettings), $oldSettings));
}
/**
* @param array<string, mixed> $kvs
*/
private function filterSettings(array $kvs): array
{
return \array_filter($kvs, static function (string $key) {
return \in_array($key, self::WEBHOOK_KEYS, true);
}, \ARRAY_FILTER_USE_KEY);
}
}