ameliabooking
/
src
/
Application
/
Commands
/
Settings
/
UpdateSettingsCategoriesCommandHandler.php
FeaturesIntegrations
2 weeks ago
GetSettingsCommand.php
1 year ago
GetSettingsCommandHandler.php
2 weeks ago
UpdateSettingsCategoriesCommand.php
6 months ago
UpdateSettingsCategoriesCommandHandler.php
2 weeks ago
UpdateSettingsCommand.php
1 year ago
UpdateSettingsCommandHandler.php
2 weeks ago
UpdateSettingsCategoriesCommandHandler.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Settings; |
| 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\Entity\User\AbstractUser; |
| 10 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 11 | use Exception; |
| 12 | use Interop\Container\Exception\ContainerException; |
| 13 | use Slim\Exception\ContainerValueNotFoundException; |
| 14 | |
| 15 | /** |
| 16 | * Class UpdateSettingsCategoriesCommandHandler |
| 17 | * |
| 18 | * @package AmeliaBooking\Application\Commands\Settings |
| 19 | */ |
| 20 | class UpdateSettingsCategoriesCommandHandler extends CommandHandler |
| 21 | { |
| 22 | /** |
| 23 | * @param UpdateSettingsCategoriesCommand $command |
| 24 | * |
| 25 | * @return CommandResult |
| 26 | * @throws AccessDeniedException |
| 27 | * @throws ContainerValueNotFoundException |
| 28 | * @throws ContainerException |
| 29 | * @throws Exception |
| 30 | */ |
| 31 | public function handle(UpdateSettingsCategoriesCommand $command) |
| 32 | { |
| 33 | $result = new CommandResult(); |
| 34 | |
| 35 | if (!$this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::SETTINGS)) { |
| 36 | /** @var AbstractUser $loggedInUser */ |
| 37 | $loggedInUser = $this->container->get('logged.in.user'); |
| 38 | |
| 39 | if ( |
| 40 | !$loggedInUser || !( |
| 41 | $loggedInUser->getType() === AbstractUser::USER_ROLE_ADMIN || |
| 42 | $loggedInUser->getType() === AbstractUser::USER_ROLE_MANAGER |
| 43 | ) |
| 44 | ) { |
| 45 | throw new AccessDeniedException('You are not allowed to write settings.'); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** @var SettingsService $settingsService */ |
| 50 | $settingsService = $this->getContainer()->get('domain.settings.service'); |
| 51 | |
| 52 | foreach ($command->getField('categories') as $category => $data) { |
| 53 | $categorySettings = $settingsService->getCategorySettings($category); |
| 54 | |
| 55 | if ($categorySettings !== null) { |
| 56 | if ($category === 'general' && array_key_exists('usedLanguages', $data)) { |
| 57 | $categorySettings['usedLanguages'] = $data['usedLanguages']; |
| 58 | unset($data['usedLanguages']); |
| 59 | } |
| 60 | |
| 61 | if ($category === 'ivy') { |
| 62 | $categorySettings = !empty($data['ivy']) ? $data['ivy'] : []; |
| 63 | } |
| 64 | |
| 65 | $categorySettings = array_replace_recursive($categorySettings, $data); |
| 66 | |
| 67 | $settingsService->setCategorySettings($category, $categorySettings); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 72 | $result->setMessage('Successfully updated settings.'); |
| 73 | |
| 74 | return $result; |
| 75 | } |
| 76 | } |
| 77 |