AddProviderCommand.php
7 years ago
AddProviderCommandHandler.php
2 years ago
GetProviderCommand.php
7 years ago
GetProviderCommandHandler.php
1 year ago
GetProvidersCommand.php
7 years ago
GetProvidersCommandHandler.php
1 year ago
UpdateProviderCommand.php
7 years ago
UpdateProviderCommandHandler.php
1 year ago
UpdateProviderStatusCommand.php
7 years ago
UpdateProviderStatusCommandHandler.php
2 years ago
GetProvidersCommandHandler.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\User\Provider; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\User\ProviderApplicationService; |
| 9 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 10 | use AmeliaBooking\Domain\Entity\Entities; |
| 11 | use AmeliaBooking\Domain\Collection\AbstractCollection; |
| 12 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 13 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 14 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 15 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 16 | use Interop\Container\Exception\ContainerException; |
| 17 | use Slim\Exception\ContainerValueNotFoundException; |
| 18 | |
| 19 | /** |
| 20 | * Class GetProvidersCommandHandler |
| 21 | * |
| 22 | * @package AmeliaBooking\Application\Commands\User\Provider |
| 23 | */ |
| 24 | class GetProvidersCommandHandler extends CommandHandler |
| 25 | { |
| 26 | /** |
| 27 | * @param GetProvidersCommand $command |
| 28 | * |
| 29 | * @return CommandResult |
| 30 | * @throws ContainerValueNotFoundException |
| 31 | * @throws QueryExecutionException |
| 32 | * @throws ContainerException |
| 33 | * @throws AccessDeniedException |
| 34 | * @throws InvalidArgumentException |
| 35 | */ |
| 36 | public function handle(GetProvidersCommand $command) |
| 37 | { |
| 38 | if (!$command->getPermissionService()->currentUserCanRead(Entities::EMPLOYEES)) { |
| 39 | throw new AccessDeniedException('You are not allowed to read employees.'); |
| 40 | } |
| 41 | |
| 42 | $result = new CommandResult(); |
| 43 | |
| 44 | /** @var ProviderRepository $providerRepository */ |
| 45 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 46 | |
| 47 | /** @var ProviderApplicationService $providerService */ |
| 48 | $providerService = $this->container->get('application.user.provider.service'); |
| 49 | /** @var SettingsService $settingsService */ |
| 50 | $settingsService = $this->container->get('domain.settings.service'); |
| 51 | |
| 52 | $itemsPerPage = $settingsService->getSetting('general', 'itemsPerPageBackEnd'); |
| 53 | $companyDaysOff = $settingsService->getCategorySettings('daysOff'); |
| 54 | |
| 55 | $params = $command->getField('params'); |
| 56 | |
| 57 | if (!$command->getPermissionService()->currentUserCanReadOthers(Entities::EMPLOYEES)) { |
| 58 | /** @var AbstractUser $currentUser */ |
| 59 | $currentUser = $this->container->get('logged.in.user'); |
| 60 | |
| 61 | $params['providers'][] = $currentUser->getId()->getValue(); |
| 62 | } |
| 63 | |
| 64 | $providers = $providerRepository->getFiltered($params, $itemsPerPage); |
| 65 | |
| 66 | if (!$providers instanceof AbstractCollection) { |
| 67 | $result->setResult(CommandResult::RESULT_ERROR); |
| 68 | $result->setMessage('Could not get users'); |
| 69 | |
| 70 | return $result; |
| 71 | } |
| 72 | |
| 73 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 74 | $result->setMessage('Successfully retrieved users.'); |
| 75 | $providers = $providers->toArray(); |
| 76 | |
| 77 | $companyDayOff = $providerService->checkIfTodayIsCompanyDayOff($companyDaysOff); |
| 78 | $providers = $providerService->manageProvidersActivity($providers, $companyDayOff); |
| 79 | |
| 80 | $providers = apply_filters('amelia_get_providers_filter', $providers); |
| 81 | |
| 82 | do_action('amelia_get_providers', $providers); |
| 83 | |
| 84 | $result->setData([ |
| 85 | Entities::USERS => $providers, |
| 86 | 'countFiltered' => (int)$providerRepository->getCount($command->getField('params')), |
| 87 | 'countTotal' => (int)$providerRepository->getCount([]), |
| 88 | ]); |
| 89 | |
| 90 | return $result; |
| 91 | } |
| 92 | } |
| 93 |