-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4529f1
commit b15dfca
Showing
10 changed files
with
149 additions
and
69 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
BROKER_CONNECT='Shabanov\Otusphp\Connection\RabbitMqConnect' | ||
BROKER_HOST='rabbitmq' | ||
BROKER_PORT=5672 | ||
BROKER_USER='guest' | ||
BROKER_PASSWORD='guest' | ||
EXCHANGE='shabanov' | ||
QUEUE='otus' | ||
|
||
EMAIL_SMTP='smtp.yandex.ru' | ||
EMAIL_PORT=465 | ||
EMAIL_USER='saveliy' | ||
EMAIL_PASSWORD='123456' | ||
EMAIL_FROM='[email protected]' | ||
EMAIL_TO='[email protected]' |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,82 +6,33 @@ | |
use PhpAmqpLib\Channel\AbstractChannel; | ||
use PhpAmqpLib\Channel\AMQPChannel; | ||
use Shabanov\Otusphp\Connection\ConnectionInterface; | ||
use Shabanov\Otusphp\Consumer\RabbitMqConsumer; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ConsumerCommand extends Command | ||
{ | ||
private string $connectClient = 'Shabanov\Otusphp\Connection\RabbitMqConnect'; | ||
private string $queue = 'otus'; | ||
private string $queue; | ||
private AMQPChannel|AbstractChannel $channel; | ||
private ConnectionInterface $connect; | ||
|
||
public function __construct(private readonly ConnectionInterface $connect) | ||
{ | ||
parent::__construct(); | ||
$this->channel = $this->connect->getClient(); | ||
$this->queue = $_ENV['QUEUE']; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('rabbitmq:consumer') | ||
->setDescription('RabbitMQ consumer обработчик формы'); | ||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$this->connect = new $this->connectClient(); | ||
$this->channel = $this->connect->getClient(); | ||
} | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$callback = function ($msg) use ($output) { | ||
/** | ||
* Выведим в консоль поступившую строку | ||
*/ | ||
$output->writeln('<info>[x] ' . $msg->body . '</info>'); | ||
/** | ||
* Отправим строку на Email | ||
*/ | ||
$this->sendEmail($msg->body); | ||
}; | ||
|
||
$this->channel->basic_consume( | ||
$this->queue, | ||
'', | ||
false, | ||
true, | ||
false, | ||
false, | ||
$callback | ||
); | ||
|
||
while ($this->channel->is_consuming()) { | ||
$this->channel->wait(); | ||
} | ||
|
||
$this->channel->close(); | ||
$this->connect->close(); | ||
(new RabbitMqConsumer($this->connect, $output))->run(); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
protected function sendEmail(string $body): void | ||
{ | ||
$mail = new \PHPMailer(true); | ||
|
||
try { | ||
$mail->isSMTP(); | ||
$mail->Host = 'smtp.yandex.ru'; | ||
$mail->SMTPAuth = true; | ||
$mail->Username = 'saveliy'; | ||
$mail->Password = 'password'; | ||
$mail->Port = 465; | ||
|
||
$mail->setFrom('[email protected]', 'Sender'); | ||
$mail->addAddress('[email protected]', 'Recipient'); | ||
|
||
$mail->Subject = 'Новое сообщение'; | ||
$mail->Body = $body; | ||
|
||
$mail->send(); | ||
} catch (Exception $e) { | ||
echo 'Ошибка отправки письма: ' . $mail->ErrorInfo; | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Shabanov\Otusphp\Consumer; | ||
|
||
use PhpAmqpLib\Channel\AbstractChannel; | ||
use PhpAmqpLib\Channel\AMQPChannel; | ||
use PhpAmqpLib\Message\AMQPMessage; | ||
use Shabanov\Otusphp\Connection\ConnectionInterface; | ||
use Shabanov\Otusphp\Mail\Mailer; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class RabbitMqConsumer | ||
{ | ||
private AMQPChannel|AbstractChannel $channel; | ||
private string $queue; | ||
|
||
public function __construct(private readonly ConnectionInterface $connect, | ||
private readonly OutputInterface $output | ||
) { | ||
$this->channel = $this->connect->getClient(); | ||
$this->queue = $_ENV['QUEUE']; | ||
} | ||
|
||
public function run(): void | ||
{ | ||
$this->channel->basic_consume( | ||
$this->queue, | ||
'', | ||
false, | ||
true, | ||
false, | ||
false, | ||
[$this, 'consumeHandler'] | ||
); | ||
|
||
while ($this->channel->is_consuming()) { | ||
$this->channel->wait(); | ||
} | ||
|
||
$this->close(); | ||
} | ||
|
||
public function consumeHandler(AMQPMessage $message): void | ||
{ | ||
/** | ||
* Выведим в консоль данные | ||
*/ | ||
$this->output->writeln('<info>[x] ' . $message->body . '</info>'); | ||
/** | ||
* Отправим строку на Email | ||
*/ | ||
(new Mailer($message->body))->send(); | ||
} | ||
|
||
private function close(): void | ||
{ | ||
$this->channel->close(); | ||
$this->connect->close(); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Shabanov\Otusphp\Mail; | ||
|
||
class Mailer | ||
{ | ||
private \PHPMailer $mail; | ||
public function __construct(private readonly string $body) | ||
{ | ||
$this->mail = new \PHPMailer(true); | ||
$this->init(); | ||
} | ||
|
||
private function init() | ||
{ | ||
$this->mail->isSMTP(); | ||
$this->mail->Host = $_ENV['EMAIL_SMTP']; | ||
$this->mail->SMTPAuth = true; | ||
$this->mail->Username = $_ENV['EMAIL_USER']; | ||
$this->mail->Password = $_ENV['EMAIL_PASSWORD']; | ||
$this->mail->Port = $_ENV['EMAIL_PORT']; | ||
|
||
$this->mail | ||
->setFrom($_ENV['EMAIL_FROM'], 'Sender') | ||
->addAddress($_ENV['EMAIL_TO'], 'Recipient'); | ||
|
||
$this->mail->Subject = 'Новое сообщение'; | ||
$this->mail->Body = $this->body; | ||
} | ||
public function send(): void | ||
{ | ||
try { | ||
$this->mail->send(); | ||
} catch (Exception $e) { | ||
echo 'Ошибка отправки письма: ' . $this->mail->ErrorInfo; | ||
} | ||
} | ||
} |
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