Skip to content

Commit

Permalink
fix: исправил замечания, вынес зависимости в конструктор
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyrcha committed May 17, 2024
1 parent d50600a commit 650d5d3
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 91 deletions.
4 changes: 1 addition & 3 deletions app/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ version: "3"

services:
nginx:
build:
context: ./nginx
dockerfile: Dockerfile
image: nginx:latest
container_name: ${NGINX_CONTAINER}
ports:
- ${NGINX_PORTS}
Expand Down
5 changes: 0 additions & 5 deletions app/nginx/Dockerfile

This file was deleted.

15 changes: 7 additions & 8 deletions app/www/application.local/commands/ConsumerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace app\commands;

use app\models\RabbitMq;
use app\models\RabbitMqConsumer;
use Exception;
use app\components\RabbitMqConsumer;
use yii\console\Controller;

class ConsumerController extends Controller
{
/**
* @throws Exception
*/
public function __construct($id, $module, private RabbitMqConsumer $rabbitMqConsumer, $config = [])
{
parent::__construct($id, $module, $config);
}

public function actionIndex(): void
{
$rabbit = new RabbitMq('bank_statement');
(new RabbitMqConsumer($rabbit))();
$this->rabbitMqConsumer->consume('bank_statement');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,28 @@

declare(strict_types=1);

namespace app\models;
namespace app\components;

use Exception;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use yii\base\Component;

class RabbitMq
class RabbitMq extends Component
{
private AMQPStreamConnection $connection;
private AMQPChannel $channel;

/**
* @throws Exception
*/
public function __construct(public string $queueName)
public function init(): void
{
parent::init();
$this->connection = new AMQPStreamConnection(
$_ENV['RABBITMQ_HOST'],
5672,
$_ENV['RABBITMQ_USER'],
$_ENV['RABBITMQ_PASS']
);
$this->channel = $this->connection->channel();
$this->queueDeclare();
}

private function queueDeclare(): void
{
$this->channel->queue_declare($this->queueName, false, true, false, false);
}

public function getChannel(): AMQPChannel
Expand Down
37 changes: 37 additions & 0 deletions app/www/application.local/components/RabbitMqConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace app\components;

use Exception;
use PhpAmqpLib\Message\AMQPMessage;
use Yii;

class RabbitMqConsumer
{
public function __construct(private RabbitMq $rabbitMq)
{
}

/**
* Чтение сообщений из RabbitMQ
*
* @param string $queueName
* @throws Exception
*/
public function consume(string $queueName): void
{
$channel = $this->rabbitMq->getChannel();
$channel->queue_declare($queueName, false, true, false, false);

$channel->basic_consume($queueName, '', false, true, false, false, [$this, 'callback']);

while ($channel->is_consuming()) {
$channel->wait();
}
}

public function callback(AMQPMessage $message): void
{
echo $message->body, "\n";
}
}
29 changes: 29 additions & 0 deletions app/www/application.local/components/RabbitMqProducer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace app\components;

use PhpAmqpLib\Message\AMQPMessage;
use Yii;

class RabbitMqProducer
{
public function __construct(private RabbitMq $rabbitMq)
{
}

public function sendMessage($queueName, $message): bool
{
try {
$channel = $this->rabbitMq->getChannel();
$channel->queue_declare($queueName, false, true, false, false);
$message = new AMQPMessage(json_encode($message));
$channel->basic_publish($message, '', $queueName);
return true;
} catch (\Exception $e) {
Yii::error('Error sending message to RabbitMQ: ' . $e->getMessage());
return false;
}
}
}
18 changes: 12 additions & 6 deletions app/www/application.local/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

namespace app\controllers;

use app\components\RabbitMqProducer;
use app\models\BankStatementForm;
use app\models\RabbitMq;
use app\models\RabbitMqProducer;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\Controller;

class SiteController extends Controller
{
public function __construct($id, $module, private RabbitMqProducer $rabbitMQ, $config = [])
{
parent::__construct($id, $module, $config);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -64,10 +68,12 @@ public function actionIndex()
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$message = ['start_time' => $model->start_time, 'end_time' => $model->end_time];

$rabbit = new RabbitMq('bank_statement');
(new RabbitMqProducer($rabbit))($message);
if ($this->rabbitMQ->sendMessage('bank_statement', $message)) {
Yii::$app->session->setFlash('success', 'Запрос принят в обработку');
} else {
Yii::$app->session->setFlash('error', 'Произошла ошибка при отправке запроса');
}

Yii::$app->session->setFlash('success', 'Запрос принят в обработку');
return $this->redirect(['index']);
}

Expand Down
35 changes: 0 additions & 35 deletions app/www/application.local/models/RabbitMqConsumer.php

This file was deleted.

22 changes: 0 additions & 22 deletions app/www/application.local/models/RabbitMqProducer.php

This file was deleted.

0 comments on commit 650d5d3

Please sign in to comment.