Skip to content

Commit d2e8c9c

Browse files
fix(email): reinforcement of the validator and skip invalid address when sending alerts
1 parent 19314cc commit d2e8c9c

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

src/Command/AlertCommand.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Console\Command\Command;
1313
use Symfony\Component\Console\Input\InputInterface;
1414
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Mime\Exception\RfcComplianceException;
1516
use Symfony\Component\Routing\RouterInterface;
1617
use Webgriffe\SyliusBackInStockNotificationPlugin\Entity\SubscriptionInterface;
1718
use Webgriffe\SyliusBackInStockNotificationPlugin\Repository\SubscriptionRepositoryInterface;
@@ -44,27 +45,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4445
//I think that this load in the long time can be a bottle necklace
4546
$subscriptions = $this->backInStockNotificationRepository->findBy(['notify' => false]);
4647
foreach ($subscriptions as $subscription) {
47-
$channel = $subscription->getChannel();
48-
$productVariant = $subscription->getProductVariant();
49-
if ($productVariant === null || $channel === null) {
50-
$this->backInStockNotificationRepository->remove($subscription);
51-
$this->logger->warning(
52-
'The back in stock subscription for the product does not have all the information required',
53-
['subscription' => var_export($subscription, true)],
54-
);
48+
try {
49+
$channel = $subscription->getChannel();
50+
$productVariant = $subscription->getProductVariant();
51+
if ($productVariant === null || $channel === null) {
52+
$this->backInStockNotificationRepository->remove($subscription);
53+
$this->logger->warning(
54+
'The back in stock subscription for the product does not have all the information required',
55+
['subscription' => var_export($subscription, true)],
56+
);
5557

56-
continue;
57-
}
58+
continue;
59+
}
5860

59-
if (
60-
$this->availabilityChecker->isStockAvailable($productVariant) &&
61-
$productVariant->isEnabled() &&
62-
$productVariant->getProduct()?->isEnabled() === true
63-
) {
64-
$this->router->getContext()->setHost($channel->getHostname() ?? 'localhost');
65-
$this->sendEmail($subscription, $productVariant, $channel);
66-
$subscription->setNotify(true);
67-
$this->backInStockNotificationRepository->add($subscription);
61+
if (
62+
$this->availabilityChecker->isStockAvailable($productVariant) &&
63+
$productVariant->isEnabled() &&
64+
$productVariant->getProduct()?->isEnabled() === true
65+
) {
66+
$this->router->getContext()->setHost($channel->getHostname() ?? 'localhost');
67+
$this->sendEmail($subscription, $productVariant, $channel);
68+
$subscription->setNotify(true);
69+
$this->backInStockNotificationRepository->add($subscription);
70+
}
71+
} catch (RfcComplianceException $e) {
72+
// Invalid email address, continue to the next one
73+
$this->logger->warning($e->getMessage());
6874
}
6975
}
7076

src/Form/SubscriptionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2525
->add('email', EmailType::class, [
2626
'constraints' => [
2727
new NotBlank([], null, null, null, ['webgriffe_sylius_back_in_stock_notification_plugin']),
28-
new Email([], null, null, null, ['webgriffe_sylius_back_in_stock_notification_plugin']),
28+
new Email(['mode' => Email::VALIDATION_MODE_STRICT], null, null, null, ['webgriffe_sylius_back_in_stock_notification_plugin']),
2929
],
3030
])
3131
->add('product_variant_code', HiddenType::class)

0 commit comments

Comments
 (0)