Skip to content

Commit 76b4611

Browse files
committed
PES-2886: Move configuration submission handling to ConfigurationFormService for improved modularity
1 parent 5991702 commit 76b4611

File tree

2 files changed

+94
-30
lines changed

2 files changed

+94
-30
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Packetery\Module;
4+
5+
use Packetery;
6+
use Packetery\Exceptions\ApiClientException;
7+
use Packetery\Exceptions\DatabaseException;
8+
use Tools;
9+
use Packetery\Tools\UserPermissionHelper;
10+
use Packetery\Tools\ConfigHelper;
11+
use Packetery\Payment\PaymentRepository;
12+
13+
class ConfigurationFormService
14+
{
15+
/** @var Packetery */
16+
private $module;
17+
18+
/**
19+
* @param Packetery $module
20+
*/
21+
public function __construct($module)
22+
{
23+
$this->module = $module;
24+
}
25+
26+
/**
27+
* @param UserPermissionHelper $userPermissionHelper
28+
* @return array<string, bool|string>
29+
* @throws ApiClientException
30+
* @throws \ReflectionException|DatabaseException
31+
*/
32+
public function handleConfigurationSubmit(UserPermissionHelper $userPermissionHelper)
33+
{
34+
$output = '';
35+
$error = false;
36+
37+
if (!$userPermissionHelper->hasPermission(UserPermissionHelper::SECTION_CONFIG, UserPermissionHelper::PERMISSION_EDIT)) {
38+
return [
39+
'output' => $this->module->displayError('You do not have permission to save configuration.'),
40+
'error' => true
41+
];
42+
}
43+
44+
$confOptions = $this->module->getConfigurationOptions();
45+
$packeteryOptions = $this->module->diContainer->get(Options::class);
46+
47+
foreach ($confOptions as $option => $optionConf) {
48+
$value = (string)Tools::getValue($option);
49+
$configValue = $packeteryOptions->formatOption($option, $value);
50+
$errorMessage = $packeteryOptions->validate($option, $configValue);
51+
if ($errorMessage !== false) {
52+
$output .= $this->module->displayError($errorMessage);
53+
$error = true;
54+
} else {
55+
ConfigHelper::update($option, $configValue);
56+
}
57+
}
58+
59+
$this->handlePaymentMethodsSubmit();
60+
61+
return [
62+
'output' => $output,
63+
'error' => $error
64+
];
65+
}
66+
67+
/**
68+
* @throws \ReflectionException
69+
* @throws DatabaseException
70+
*/
71+
private function handlePaymentMethodsSubmit()
72+
{
73+
$paymentRepository = $this->module->diContainer->get(PaymentRepository::class);
74+
$paymentList = $paymentRepository->getListPayments();
75+
76+
if ($paymentList) {
77+
foreach ($paymentList as $payment) {
78+
if (Tools::getIsset('payment_cod_' . $payment['module_name'])) {
79+
$paymentRepository->setOrInsert(1, $payment['module_name']);
80+
} else {
81+
$paymentRepository->setOrInsert(0, $payment['module_name']);
82+
}
83+
}
84+
}
85+
}
86+
}

packetery/packetery.php

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public function getCarriersContent()
245245
* @throws PrestaShopException
246246
* @throws ReflectionException
247247
* @throws \Packetery\Exceptions\DatabaseException
248+
* @throws \Packetery\Exceptions\ApiClientException
248249
*/
249250
public function getContent()
250251
{
@@ -299,36 +300,13 @@ public function getContent()
299300
}
300301

301302
if (Tools::isSubmit('submit' . $this->name)) {
302-
if (!$userPermissionHelper->hasPermission(\Packetery\Tools\UserPermissionHelper::SECTION_CONFIG, \Packetery\Tools\UserPermissionHelper::PERMISSION_EDIT)) {
303-
$output .= $this->displayError('You do not have permission to save configuration.');
303+
$isSubmit = true;
304+
/** @var \Packetery\Module\ConfigurationFormService $configurationFormService */
305+
$configurationFormService = $this->diContainer->get(\Packetery\Module\ConfigurationFormService::class);
306+
$result = $configurationFormService->handleConfigurationSubmit($userPermissionHelper);
307+
$output .= $result['output'];
308+
if ($result['error']) {
304309
$error = true;
305-
} else {
306-
$isSubmit = true;
307-
$confOptions = $this->getConfigurationOptions();
308-
/** @var \Packetery\Module\Options $packeteryOptions */
309-
$packeteryOptions = $this->diContainer->get(\Packetery\Module\Options::class);
310-
foreach ($confOptions as $option => $optionConf) {
311-
$value = (string)Tools::getValue($option);
312-
$configValue = $packeteryOptions->formatOption($option, $value);
313-
$errorMessage = $packeteryOptions->validate($option, $configValue);
314-
if ($errorMessage !== false) {
315-
$output .= $this->displayError($errorMessage);
316-
$error = true;
317-
} else {
318-
\Packetery\Tools\ConfigHelper::update($option, $configValue);
319-
}
320-
}
321-
$paymentRepository = $this->diContainer->get(\Packetery\Payment\PaymentRepository::class);
322-
$paymentList = $paymentRepository->getListPayments();
323-
if ($paymentList) {
324-
foreach ($paymentList as $payment) {
325-
if (Tools::getIsset('payment_cod_' . $payment['module_name'])) {
326-
$paymentRepository->setOrInsert(1, $payment['module_name']);
327-
} else {
328-
$paymentRepository->setOrInsert(0, $payment['module_name']);
329-
}
330-
}
331-
}
332310
}
333311
}
334312

@@ -507,7 +485,7 @@ private function generateCronInfoBlock()
507485
return $this->context->smarty->fetch($this->local_path . 'views/templates/admin/generateCronInfoBlock.tpl');
508486
}
509487

510-
private function getConfigurationOptions()
488+
public function getConfigurationOptions()
511489
{
512490
return [
513491
'PACKETERY_APIPASS' => [

0 commit comments

Comments
 (0)