Last active
December 19, 2020 23:41
-
-
Save 94noni/fb8251637c47be0101d7854f1e33aa78 to your computer and use it in GitHub Desktop.
MugAlertCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Command; | |
use Psr\Log\LoggerInterface; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\DomCrawler\Crawler; | |
use Symfony\Component\Mailer\MailerInterface; | |
use Symfony\Component\Mime\Email; | |
use Symfony\Component\Notifier\Message\SmsMessage; | |
use Symfony\Component\Notifier\TexterInterface; | |
use Symfony\Contracts\HttpClient\HttpClientInterface; | |
use function Symfony\Component\String\u; | |
class MugAlertCommand extends Command | |
{ | |
protected static $defaultName = 'app:mug-alert'; | |
private HttpClientInterface $httpClient; | |
private TexterInterface $texter; | |
private MailerInterface $mailer; | |
private LoggerInterface $logger; | |
public function __construct(HttpClientInterface $httpClient, TexterInterface $texter, MailerInterface $mailer, LoggerInterface $logger) | |
{ | |
$this->httpClient = $httpClient; | |
$this->texter = $texter; | |
$this->mailer = $mailer; | |
$this->logger = $logger; | |
parent::__construct(); | |
} | |
protected function configure() | |
{ | |
$this->setDescription('Mug alert'); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
if ($this->crawlIsMugInStock()) { | |
// send alerts! | |
$message = \sprintf('Mug Noel dispo ! (%s)', \date('Y-m-d H:i:s')); | |
$this->sendSms($message); // to me | |
$this->sendMail($message); // to her | |
} | |
return Command::SUCCESS; | |
} | |
// use first the HttpClient to get the HTML | |
// then the DomCrawler to get the proper id element and its text content | |
// finaly check the wording to decide if it is ok|ko | |
private function crawlIsMugInStock(): bool | |
{ | |
$response = $this->httpClient->request( | |
'GET', | |
'https://foobar.myshopify.com/products/product?variant=123' | |
); | |
if (200 !== $statusCode = $response->getStatusCode()) { | |
// handle failure via log and alert | |
$this->logger->error('Status code KO', ['status_code' => $statusCode]); | |
$this->sendSms(\sprintf('Status code KO (%s)', $statusCode)); | |
return false; | |
} | |
$crawler = new Crawler($response->getContent()); | |
$selector = $crawler->filter('#ProductSelect-product-template'); // the element inside the page | |
$soldOut = u($selector->text())->lower()->containsAny('épuisé'); // "épuisé" means sold out | |
return !$soldOut; | |
} | |
// use the Texter Notifier configured with FreeMobile (SMS) to send an SMS | |
private function sendSms(string $message): void | |
{ | |
$this->texter->send(new SmsMessage('0102030405', $message)); | |
} | |
// use the Mailer to send a raw mail | |
private function sendMail(string $message): void | |
{ | |
$email = (new Email()) | |
->from('[email protected]') | |
->to('[email protected]') | |
->subject($message) | |
->text($message); | |
$this->mailer->send($email); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment