ameliabooking
/
src
/
Application
/
Commands
/
Settings
/
FeaturesIntegrations
/
ToggleFeatureIntegrationCommandHandler.php
ToggleFeatureIntegrationCommand.php
6 months ago
ToggleFeatureIntegrationCommandHandler.php
6 months ago
ToggleFeatureIntegrationCommandHandler.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Settings\FeaturesIntegrations; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 8 | |
| 9 | class ToggleFeatureIntegrationCommandHandler extends CommandHandler |
| 10 | { |
| 11 | public $mandatoryFields = [ |
| 12 | 'code' |
| 13 | ]; |
| 14 | |
| 15 | public function handle(ToggleFeatureIntegrationCommand $command) |
| 16 | { |
| 17 | $result = new CommandResult(); |
| 18 | |
| 19 | $this->checkMandatoryFields($command); |
| 20 | |
| 21 | $code = $command->getField('code'); |
| 22 | |
| 23 | /** @var SettingsService $settingsService */ |
| 24 | $settingsService = $this->getContainer()->get('domain.settings.service'); |
| 25 | |
| 26 | $payments = $settingsService->getCategorySettings('payments'); |
| 27 | |
| 28 | $featuresIntegrations = $settingsService->getCategorySettings('featuresIntegrations'); |
| 29 | |
| 30 | if (!isset($featuresIntegrations[$code])) { |
| 31 | $result->setResult(CommandResult::RESULT_ERROR); |
| 32 | $result->setMessage('Feature or integration does not exist.'); |
| 33 | |
| 34 | return $result; |
| 35 | } |
| 36 | |
| 37 | $featuresIntegrations[$code]['enabled'] = !$featuresIntegrations[$code]['enabled']; |
| 38 | $settingsService->setCategorySettings('featuresIntegrations', $featuresIntegrations); |
| 39 | |
| 40 | if ( |
| 41 | isset($payments[$code]['enabled'], $featuresIntegrations[$code]) && |
| 42 | !$featuresIntegrations[$code]['enabled'] && |
| 43 | $payments[$code]['enabled'] |
| 44 | ) { |
| 45 | $defaultPaymentMethod = 'onSite'; |
| 46 | |
| 47 | $payments[$code]['enabled'] = false; |
| 48 | |
| 49 | foreach (['stripe', 'payPal', 'wc', 'mollie', 'razorpay', 'square', 'barion'] as $method) { |
| 50 | if ($featuresIntegrations[$method]['enabled'] && $payments[$method]['enabled']) { |
| 51 | $defaultPaymentMethod = $method; |
| 52 | |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if ($defaultPaymentMethod === 'onSite') { |
| 58 | $payments['onSite'] = true; |
| 59 | } |
| 60 | |
| 61 | $payments['defaultPaymentMethod'] = $defaultPaymentMethod; |
| 62 | |
| 63 | $settingsService->setCategorySettings('payments', $payments); |
| 64 | } |
| 65 | |
| 66 | return $result; |
| 67 | } |
| 68 | } |
| 69 |