-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAlertCommand.php
More file actions
98 lines (87 loc) · 3.89 KB
/
AlertCommand.php
File metadata and controls
98 lines (87 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
namespace Webgriffe\SyliusBackInStockNotificationPlugin\Command;
use Psr\Log\LoggerInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Mime\Exception\RfcComplianceException;
use Symfony\Component\Routing\RouterInterface;
use Webgriffe\SyliusBackInStockNotificationPlugin\Entity\SubscriptionInterface;
use Webgriffe\SyliusBackInStockNotificationPlugin\Repository\SubscriptionRepositoryInterface;
final class AlertCommand extends Command
{
protected static $defaultName = 'webgriffe:back-in-stock-notification:alert';
public function __construct(
private LoggerInterface $logger,
private SenderInterface $sender,
private AvailabilityCheckerInterface $availabilityChecker,
private SubscriptionRepositoryInterface $backInStockNotificationRepository,
private RouterInterface $router,
string $name = null,
) {
parent::__construct($name);
}
protected function configure(): void
{
$this
->setDescription('Send an email to the user if the product is returned in stock')
->setHelp('Check the stock status of the products in the webgriffe_back_in_stock_notification table and send and email to the user if the product is returned in stock')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
//I think that this load in the long time can be a bottle necklace
$subscriptions = $this->backInStockNotificationRepository->findBy(['notify' => false]);
foreach ($subscriptions as $subscription) {
try {
$channel = $subscription->getChannel();
$productVariant = $subscription->getProductVariant();
if ($productVariant === null || $channel === null) {
$this->backInStockNotificationRepository->remove($subscription);
$this->logger->warning(
'The back in stock subscription for the product does not have all the information required',
['subscription' => var_export($subscription, true)],
);
continue;
}
if (
$this->availabilityChecker->isStockAvailable($productVariant) &&
$productVariant->isEnabled() &&
$productVariant->getProduct()?->isEnabled() === true
) {
$this->router->getContext()->setHost($channel->getHostname() ?? 'localhost');
$this->sendEmail($subscription, $productVariant, $channel);
$subscription->setNotify(true);
$this->backInStockNotificationRepository->add($subscription);
}
} catch (RfcComplianceException $e) {
// Invalid email address, continue to the next one
$this->logger->warning($e->getMessage());
}
}
return 0;
}
private function sendEmail(
SubscriptionInterface $subscription,
ProductVariantInterface $productVariant,
ChannelInterface $channel,
): void {
$this->sender->send(
'webgriffe_back_in_stock_notification_alert',
[$subscription->getEmail()],
[
'subscription' => $subscription,
'product' => $productVariant->getProduct(),
'channel' => $channel,
'localeCode' => $subscription->getLocaleCode(),
],
[],
[],
);
}
}