Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# PHP2021
# PHP2021

## Домашняя работа № 17. Api

### Запуск приложения

Отправка сообщений в очередь через форму по адресу `http://otus.local/post_request.php`
Форма отправляет данные на `/api/v1/message`

Получение информации о статусе сообщения `/api/v1/message/{messageId}`

Запуск обработчика сообщений через кнопку по адресу `http://otus.local/consume.php`

Документация Swagger-Ui по адресу `http://otus.local:8083/`
28 changes: 28 additions & 0 deletions code/app_src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App;

use App\Infrastructure\RequestHandler;
use App\Helpers\AppHelper;
use App\Application\Interfaces\StorageInterface;
use Exception;

class Application
{
private RequestHandler $handler;

public function __construct()
{
try {
$storage = AppHelper::getStorageClient();
$this->handler = new RequestHandler($storage);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}

public function run(): void
{
$this->handler->execute();
}
}
21 changes: 21 additions & 0 deletions code/app_src/Application/Adapters/RabbitAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Application\Adapters;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use App\Application\Interfaces\QueueInterface;

class RabbitAdapter implements QueueInterface
{
public AMQPStreamConnection $connection;

public function __construct()
{
$this->connection = new AMQPStreamConnection(
getenv('RABBITMQ_DEFAULT_NAME'),
getenv('RABBITMQ_DEFAULT_PORT'),
getenv('RABBITMQ_DEFAULT_USER'),
getenv('RABBITMQ_DEFAULT_PASS')
);
}
}
9 changes: 9 additions & 0 deletions code/app_src/Application/Interfaces/BankServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Application\Interfaces;


interface BankServiceInterface
{
public function getUserData();
}
10 changes: 10 additions & 0 deletions code/app_src/Application/Interfaces/ConsumerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Application\Interfaces;

interface ConsumerInterface
{
public function runFromQueue(string $queueName): void;

public function closeConnection(): void;
}
8 changes: 8 additions & 0 deletions code/app_src/Application/Interfaces/HttpHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Application\Interfaces;

interface HttpHandlerInterface
{
//
}
8 changes: 8 additions & 0 deletions code/app_src/Application/Interfaces/MailAgentInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Application\Interfaces;

interface MailAgentInterface
{
public function send(string $to, string $subject, string $body): bool;
}
10 changes: 10 additions & 0 deletions code/app_src/Application/Interfaces/PublisherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Application\Interfaces;

interface PublisherInterface
{
public function addToQueue(array $request, string $queueName): string;

public function closeConnection(): void;
}
8 changes: 8 additions & 0 deletions code/app_src/Application/Interfaces/QueueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Application\Interfaces;

interface QueueInterface
{

}
12 changes: 12 additions & 0 deletions code/app_src/Application/Interfaces/StorageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Application\Interfaces;

interface StorageInterface
{
public function insert(string $eventId): string;

public function searchById(string $eventId): ?string;

public function update(string $eventId, string $status): void;
}
39 changes: 39 additions & 0 deletions code/app_src/Domain/BankStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Domain;

class BankStatement
{
private string $dateFrom;
private string $dateTo;
private string $clientId;
private string $clientMail;

public function __construct(string $dateFrom, string $dateto, string $clientId, string $clientMail)
{
$this->dateFrom = $dateFrom;
$this->dateTo = $dateto;
$this->clientId = $clientId;
$this->clientMail = $clientMail;
}

public function getDateFrom(): string
{
return $this->dateFrom;
}

public function getDateTo(): string
{
return $this->dateTo;
}

public function getClientId(): string
{
return $this->clientId;
}

public function getClientMail(): string
{
return $this->clientMail;
}
}
37 changes: 37 additions & 0 deletions code/app_src/Domain/RedisStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Domain;

use App\Application\Interfaces\StorageInterface;
use \Predis\Client as RedisClient;

class RedisStorage implements StorageInterface
{
private RedisClient $client;

public function __construct()
{
$this->client = new RedisClient([
'scheme' => 'tcp',
'host' => getenv('REDIS_HOST'),
'port' => getenv('REDIS_PORT'),
]);
}

public function insert(string $eventId): string
{
$this->client->set("event:$eventId:status", 'running');

return $eventId;
}

public function update(string $eventId, string $status): void
{
$this->client->set("event:$eventId:status", $status);
}

public function searchById(string $eventId): ?string
{
return $this->client->get("event:$eventId:status");
}
}
34 changes: 34 additions & 0 deletions code/app_src/Helpers/AppHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Helpers;

use App\Infrastructure\BankService;
use App\Infrastructure\Consumer;
use App\Infrastructure\Publisher;
use App\Infrastructure\MailAgent;
use App\Application\Interfaces\QueueInterface;
use App\Application\Interfaces\StorageInterface;
use App\Domain\RedisStorage;
use PHPMailer\PHPMailer\PHPMailer;

class AppHelper
{
public static function createPublisher(QueueInterface $adapter): Publisher
{
return new Publisher($adapter->connection);
}

public static function createConsumer(QueueInterface $adapter, StorageInterface $storageClient): Consumer
{
return new Consumer(
$adapter->connection,
new BankService(),
$storageClient
);
}

public static function getStorageClient(): StorageInterface
{
return new RedisStorage();
}
}
64 changes: 64 additions & 0 deletions code/app_src/Infrastructure/BankService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Infrastructure;

use App\Domain\BankStatement;
use App\Application\Interfaces\BankServiceInterface;

class BankService implements BankServiceInterface
{

private BankStatement $bankStatement;
private array $requiredStatementFields = [
'date_from',
'date_to',
'client_id',
'client_email',
];

public function setBankStatement(string $userData): void
{
$statement = json_decode($userData, true);

if ($this->validateStatementData($statement)) {
throw new Exception('Not enough data for request');
}

$this->bankStatement = new BankStatement(
$statement['date_from'],
$statement['date_to'],
$statement['client_id'],
$statement['client_email']
);
}

public function validateStatementData(array $data): bool
{
if (is_null($data)) {
return false;
}

foreach ($this->requiredStatementFields as $field){
if (!in_array($field, $data)) {
return false;
}
}

return true;
}

public function getUserData(): array
{
if (isset($this->bankStatement)) {
//некоторая выборка данных за указанный пользователем период
$someClientInfo = range(0,100);
shuffle($someClientInfo);
return [
'client_mail' => $this->bankStatement->getClientMail(),
'client_info' => json_encode($someClientInfo)
];
} else {
throw new Exception('Empty statement');
}
}
}
Loading