ameliabooking
/
src
/
Application
/
Commands
/
Settings
/
FeaturesIntegrations
/
ToggleFeatureIntegrationCommandHandler.php
ToggleFeatureIntegrationCommand.php
6 months ago
ToggleFeatureIntegrationCommandHandler.php
2 weeks ago
ToggleFeatureIntegrationCommandHandler.php
96 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\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Domain\Entity\Entities; |
| 9 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 10 | use AmeliaBooking\Infrastructure\WP\Integrations\PluginInstaller; |
| 11 | |
| 12 | class ToggleFeatureIntegrationCommandHandler extends CommandHandler |
| 13 | { |
| 14 | public $mandatoryFields = [ |
| 15 | 'code' |
| 16 | ]; |
| 17 | |
| 18 | public function handle(ToggleFeatureIntegrationCommand $command) |
| 19 | { |
| 20 | $result = new CommandResult(); |
| 21 | |
| 22 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::SETTINGS)) { |
| 23 | throw new AccessDeniedException('You are not allowed to write settings.'); |
| 24 | } |
| 25 | |
| 26 | $this->checkMandatoryFields($command); |
| 27 | |
| 28 | $code = $command->getField('code'); |
| 29 | |
| 30 | /** @var SettingsService $settingsService */ |
| 31 | $settingsService = $this->getContainer()->get('domain.settings.service'); |
| 32 | |
| 33 | $payments = $settingsService->getCategorySettings('payments'); |
| 34 | |
| 35 | $featuresIntegrations = $settingsService->getCategorySettings('featuresIntegrations'); |
| 36 | |
| 37 | if (!isset($featuresIntegrations[$code])) { |
| 38 | $result->setResult(CommandResult::RESULT_ERROR); |
| 39 | $result->setMessage('Feature or integration does not exist.'); |
| 40 | |
| 41 | return $result; |
| 42 | } |
| 43 | |
| 44 | if ($code === 'ivy' && (!PluginInstaller::isPluginInstalled('ivyforms') || !PluginInstaller::isPluginActive('ivyforms'))) { |
| 45 | $ivyResult = []; |
| 46 | |
| 47 | // if IvyForms is not installed or not active, install and activate it |
| 48 | if (!PluginInstaller::isPluginInstalled('ivyforms')) { |
| 49 | $ivyResult = PluginInstaller::installAndActivatePlugin('ivyforms'); |
| 50 | } elseif (!PluginInstaller::isPluginActive('ivyforms')) { |
| 51 | $ivyResult = PluginInstaller::activatePlugin('ivyforms'); |
| 52 | } |
| 53 | |
| 54 | if (empty($ivyResult['success'])) { |
| 55 | $result->setResult(CommandResult::RESULT_ERROR); |
| 56 | $result->setMessage($ivyResult['message']); |
| 57 | |
| 58 | return $result; |
| 59 | } |
| 60 | |
| 61 | $result->setMessage($ivyResult['message']); |
| 62 | } |
| 63 | |
| 64 | $featuresIntegrations[$code]['enabled'] = !$featuresIntegrations[$code]['enabled']; |
| 65 | $settingsService->setCategorySettings('featuresIntegrations', $featuresIntegrations); |
| 66 | |
| 67 | if ( |
| 68 | isset($payments[$code]['enabled'], $featuresIntegrations[$code]) && |
| 69 | !$featuresIntegrations[$code]['enabled'] && |
| 70 | $payments[$code]['enabled'] |
| 71 | ) { |
| 72 | $defaultPaymentMethod = 'onSite'; |
| 73 | |
| 74 | $payments[$code]['enabled'] = false; |
| 75 | |
| 76 | foreach (['stripe', 'payPal', 'wc', 'mollie', 'razorpay', 'square', 'barion'] as $method) { |
| 77 | if ($featuresIntegrations[$method]['enabled'] && $payments[$method]['enabled']) { |
| 78 | $defaultPaymentMethod = $method; |
| 79 | |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if ($defaultPaymentMethod === 'onSite') { |
| 85 | $payments['onSite'] = true; |
| 86 | } |
| 87 | |
| 88 | $payments['defaultPaymentMethod'] = $defaultPaymentMethod; |
| 89 | |
| 90 | $settingsService->setCategorySettings('payments', $payments); |
| 91 | } |
| 92 | |
| 93 | return $result; |
| 94 | } |
| 95 | } |
| 96 |