Skip to content

Commit

Permalink
ДЗ №19: Правки в ревью
Browse files Browse the repository at this point in the history
  • Loading branch information
shabanovvv committed Mar 13, 2024
1 parent a4529f1 commit b15dfca
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 69 deletions.
14 changes: 14 additions & 0 deletions hw19/www/.env.example
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]'
6 changes: 5 additions & 1 deletion hw19/www/bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ require __DIR__ . '/../vendor/autoload.php';

use Shabanov\Otusphp\Command\ConsumerCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/../.env');

$application = new Application();
$application->add(new ConsumerCommand());
$application->add(new ConsumerCommand(new $_ENV['BROKER_CONNECT']()));
try {
$application->run();
} catch (Exception $e) {
Expand Down
1 change: 1 addition & 0 deletions hw19/www/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"php-amqplib/php-amqplib": "^3.6",
"symfony/console": "^7.0",
"symfony/mailer": "^7.0",
"symfony/dotenv": "^7.0",
"phpmailer/phpmailer": "^6.9"
}
}
7 changes: 6 additions & 1 deletion hw19/www/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@

namespace Shabanov\Otusphp;

use Symfony\Component\Dotenv\Dotenv;

class App
{
public function __construct()
{}
{
$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/../.env');
}

public function __invoke(): void
{
Expand Down
69 changes: 10 additions & 59 deletions hw19/www/src/Command/ConsumerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
7 changes: 6 additions & 1 deletion hw19/www/src/Connection/RabbitMqConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class RabbitMqConnect implements ConnectionInterface
*/
public function __construct()
{
$this->connect = new AMQPStreamConnection('rabbitmq', 5672, 'guest', 'guest');
$this->connect = new AMQPStreamConnection(
$_ENV['BROKER_HOST'],
$_ENV['BROKER_PORT'],
$_ENV['BROKER_USER'],
$_ENV['BROKER_PASSWORD']
);
}
public function getClient(): AMQPChannel|AbstractChannel
{
Expand Down
61 changes: 61 additions & 0 deletions hw19/www/src/Consumer/RabbitMqConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

Check failure on line 1 in hw19/www/src/Consumer/RabbitMqConsumer.php

View workflow job for this annotation

GitHub Actions / phpcs

Header blocks must be separated by a single blank line
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,

Check failure on line 18 in hw19/www/src/Consumer/RabbitMqConsumer.php

View workflow job for this annotation

GitHub Actions / phpcs

The first parameter of a multi-line function declaration must be on the line after the opening bracket
private readonly OutputInterface $output

Check failure on line 19 in hw19/www/src/Consumer/RabbitMqConsumer.php

View workflow job for this annotation

GitHub Actions / phpcs

Multi-line function declaration not indented correctly; expected 8 spaces but found 32
) {
$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();
}
}
4 changes: 3 additions & 1 deletion hw19/www/src/Controller/PageController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

Check failure on line 1 in hw19/www/src/Controller/PageController.php

View workflow job for this annotation

GitHub Actions / phpcs

Header blocks must be separated by a single blank line
declare(strict_types=1);

namespace Shabanov\Otusphp\Controller;

Expand All @@ -17,7 +18,8 @@ public function formHandler(): void
{
if (!empty($_REQUEST['send']) && !empty($_REQUEST['date_from'])) {
$message = 'Date from: ' . $_REQUEST['date_from'] . ' Date to: ' . $_REQUEST['date_to'];
(new RabbitMqProducer())->send($message);
(new RabbitMqProducer(new $_ENV['BROKER_CONNECT']()))
->send($message);
echo (new FormSuccessRender())->show();
}
}
Expand Down
38 changes: 38 additions & 0 deletions hw19/www/src/Mail/Mailer.php
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;
}
}
}
11 changes: 5 additions & 6 deletions hw19/www/src/Producer/RabbitMqProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@

class RabbitMqProducer
{
private string $connectClient = 'Shabanov\Otusphp\Connection\RabbitMqConnect';
private AMQPChannel|AbstractChannel $channel;
private string $exchange = 'shabanov';
private string $queue = 'otus';
private ConnectionInterface $connect;
private string $exchange;
private string $queue;

public function __construct()
public function __construct(private ConnectionInterface $connect)
{
$this->connect = new $this->connectClient();
$this->channel = $this->connect->getClient();
$this->exchange = $_ENV['EXCHANGE'];
$this->queue = $_ENV['QUEUE'];
}

public function send(string $message): void
Expand Down

0 comments on commit b15dfca

Please sign in to comment.