| 1 |
<?php |
| 2 |
|
| 3 |
namespace AmeliaBooking\Application\Commands\Notification; |
| 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\Helper\HelperService; |
| 9 |
use AmeliaBooking\Application\Services\Placeholder\PlaceholderService; |
| 10 |
use AmeliaBooking\Application\Services\Notification\EmailNotificationService; |
| 11 |
use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 12 |
use AmeliaBooking\Domain\Entity\Entities; |
| 13 |
use AmeliaBooking\Application\Services\Settings\SettingsService; |
| 14 |
use AmeliaBooking\Domain\ValueObjects\String\NotificationSendTo; |
| 15 |
use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 16 |
use AmeliaBooking\Infrastructure\Services\Notification\MailgunService; |
| 17 |
use AmeliaBooking\Infrastructure\Services\Notification\OutlookService; |
| 18 |
use AmeliaBooking\Infrastructure\Services\Notification\PHPMailService; |
| 19 |
use AmeliaBooking\Infrastructure\Services\Notification\SMTPService; |
| 20 |
use Exception; |
| 21 |
use Slim\Exception\ContainerValueNotFoundException; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class SendTestEmailCommandHandler |
| 25 |
* |
| 26 |
* @package AmeliaBooking\Application\Commands\Notification |
| 27 |
*/ |
| 28 |
class SendTestEmailCommandHandler extends CommandHandler |
| 29 |
{ |
| 30 |
public $mandatoryFields = [ |
| 31 |
'notificationTemplate', |
| 32 |
'recipientEmail' |
| 33 |
]; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param SendTestEmailCommand $command |
| 37 |
* |
| 38 |
* @return CommandResult |
| 39 |
* @throws ContainerValueNotFoundException |
| 40 |
* @throws AccessDeniedException |
| 41 |
* @throws InvalidArgumentException |
| 42 |
* @throws QueryExecutionException |
| 43 |
* @throws Exception |
| 44 |
*/ |
| 45 |
public function handle(SendTestEmailCommand $command) |
| 46 |
{ |
| 47 |
if (!$command->getPermissionService()->currentUserCanWrite(Entities::NOTIFICATIONS)) { |
| 48 |
throw new AccessDeniedException('You are not allowed to send test email'); |
| 49 |
} |
| 50 |
|
| 51 |
$result = new CommandResult(); |
| 52 |
|
| 53 |
$this->checkMandatoryFields($command); |
| 54 |
|
| 55 |
$type = $command->getField('type'); |
| 56 |
|
| 57 |
/** @var PHPMailService|SMTPService|MailgunService|OutlookService $mailService */ |
| 58 |
$mailService = $this->getContainer()->get('infrastructure.mail.service'); |
| 59 |
/** @var EmailNotificationService $notificationService */ |
| 60 |
$notificationService = $this->getContainer()->get('application.emailNotification.service'); |
| 61 |
/** @var PlaceholderService $placeholderService */ |
| 62 |
$placeholderService = $this->getContainer()->get("application.placeholder.{$type}.service"); |
| 63 |
/** @var SettingsService $settingsAS*/ |
| 64 |
$settingsAS = $this->container->get('application.settings.service'); |
| 65 |
/** @var \AmeliaBooking\Domain\Services\Settings\SettingsService $settingsService */ |
| 66 |
$settingsService = $this->container->get('domain.settings.service'); |
| 67 |
/** @var HelperService $helperService */ |
| 68 |
$helperService = $this->container->get('application.helper.service'); |
| 69 |
|
| 70 |
$notificationSettings = $settingsService->getCategorySettings('notifications'); |
| 71 |
$appointmentsSettings = $settingsService->getCategorySettings('appointments'); |
| 72 |
|
| 73 |
if (!$notificationSettings['senderEmail'] || !$notificationSettings['senderName']) { |
| 74 |
$result->setResult(CommandResult::RESULT_ERROR); |
| 75 |
$result->setMessage('Test email not sent'); |
| 76 |
|
| 77 |
return $result; |
| 78 |
} |
| 79 |
|
| 80 |
$notification = $notificationService->getById($command->getField('notificationTemplate')); |
| 81 |
|
| 82 |
$dummyData = $placeholderService->getPlaceholdersDummyData('email'); |
| 83 |
|
| 84 |
$isForCustomer = $notification->getSendTo()->getValue() === NotificationSendTo::CUSTOMER; |
| 85 |
$placeholderStringRec = 'recurring' . 'Placeholders' . ($isForCustomer ? 'Customer' : ''); |
| 86 |
$placeholderStringPack = 'package' . 'Placeholders' . ($isForCustomer ? 'Customer' : ''); |
| 87 |
|
| 88 |
$dummyData['recurring_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringRec], $dummyData); |
| 89 |
$dummyData['package_appointments_details'] = $placeholderService->applyPlaceholders($appointmentsSettings[$placeholderStringPack], $dummyData); |
| 90 |
|
| 91 |
|
| 92 |
$language = $command->getField('language'); |
| 93 |
$info = json_encode(['locale' => $language]); |
| 94 |
$notificationSubject = $helperService->getBookingTranslation( |
| 95 |
$helperService->getLocaleFromBooking($info), |
| 96 |
$notification->getTranslations() ? $notification->getTranslations()->getValue() : null, |
| 97 |
'subject' |
| 98 |
) ?: $notification->getSubject()->getValue(); |
| 99 |
|
| 100 |
$notificationContent = $helperService->getBookingTranslation( |
| 101 |
$helperService->getLocaleFromBooking($info), |
| 102 |
$notification->getTranslations() ? $notification->getTranslations()->getValue() : null, |
| 103 |
'content' |
| 104 |
) ?: $notification->getContent()->getValue(); |
| 105 |
|
| 106 |
$subject = $placeholderService->applyPlaceholders( |
| 107 |
$notificationSubject, |
| 108 |
$dummyData |
| 109 |
); |
| 110 |
|
| 111 |
$content = $placeholderService->applyPlaceholders( |
| 112 |
$notificationContent, |
| 113 |
$dummyData |
| 114 |
); |
| 115 |
|
| 116 |
$emailData = apply_filters( |
| 117 |
'amelia_manipulate_test_email_data', |
| 118 |
[ |
| 119 |
'email' => $command->getField('recipientEmail'), |
| 120 |
'subject' => $subject, |
| 121 |
'body' => $notificationService->getParsedBody($content), |
| 122 |
'bcc' => $settingsAS->getBccEmails() |
| 123 |
] |
| 124 |
); |
| 125 |
|
| 126 |
do_action('amelia_before_send_test_email', $emailData); |
| 127 |
|
| 128 |
if (empty($emailData['skipSending'])) { |
| 129 |
$mailService->send( |
| 130 |
$emailData['email'], |
| 131 |
$emailData['subject'], |
| 132 |
$emailData['body'], |
| 133 |
$emailData['bcc'] |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
do_action('amelia_after_send_test_email', $emailData); |
| 138 |
|
| 139 |
$result->setResult(CommandResult::RESULT_SUCCESS); |
| 140 |
$result->setMessage('Test email successfully sent'); |
| 141 |
|
| 142 |
return $result; |
| 143 |
} |
| 144 |
} |
| 145 |
|