добавляю все изменения проекта на текущий момент
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use App\Facades\UserContext;
|
||||
use Exception;
|
||||
|
||||
class RedisNotificationService extends RedisBaseService
|
||||
{
|
||||
protected string $connection = 'notifications';
|
||||
/**
|
||||
* @var array тексты уведомлений
|
||||
*/
|
||||
public array $notificationTexts = [];
|
||||
/**
|
||||
* @var array тексты нотификаций
|
||||
*/
|
||||
public array $notificationTypes = [];
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->connection('notifications');
|
||||
}
|
||||
|
||||
|
||||
protected function connection($connection): void
|
||||
{
|
||||
$this->connection = $connection;
|
||||
//Предполагается, что время жизни должно быть небольшое, так уведомления размещаются перед самим редиректом для их демонстрации пользователю как можно скорее
|
||||
$this->ttl = 180;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Метод возвращает id юзера, который является обязательной частью ключа в БД redis с нотификациями
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function identifyUser(): int
|
||||
{
|
||||
$userId = UserContext::getUserId();
|
||||
|
||||
return $userId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Устанавливаем текст уведомлений для отправки в Redis
|
||||
*
|
||||
* @param array $notificationTexts массив текстов уведомлений
|
||||
* @return self
|
||||
*/
|
||||
public function notifications(array $notificationTexts): self
|
||||
{
|
||||
$this->notificationTexts = $notificationTexts;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Устанавливаем типы для передаваемых Redis уведомлений
|
||||
*
|
||||
* @param array $notificationTypes типы уведомлений
|
||||
* @return self
|
||||
*/
|
||||
public function types(array $notificationTypes): self
|
||||
{
|
||||
//Если при передаче типов уведомлений не указаны тексты уведомлений, либо если количество текстов уведомлений не равно количеству типов уведомлений, возвращаем ошибку
|
||||
if (empty($this->notificationTexts)) {
|
||||
throw new Exception('Не переданы тексты уведомлений');
|
||||
}
|
||||
if (count($this->notificationTexts) !== count($notificationTypes)) {
|
||||
throw new Exception('Количество переданных типов должно совпадать с количеством текстов уведомлений');
|
||||
}
|
||||
if ($incorrectTypes = array_diff($notificationTypes, config('app.FRONT_notification_types'))) {
|
||||
throw new Exception('Переданные типы уведомлений ' . implode(', ', $incorrectTypes) . ' не существуют');
|
||||
}
|
||||
$this->notificationTypes = $notificationTypes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function put(): void
|
||||
{
|
||||
$notificationsRedis = [];
|
||||
if (empty($this->notificationTexts) || empty($this->notificationTypes)) {
|
||||
throw new Exception('Отсутствуют обязательные параметры для отправки в Redis');
|
||||
} else {
|
||||
foreach ($this->notificationTexts as $key => $text) {
|
||||
$notificationsRedis[] = ['text' => $text, 'type' => $this->notificationTypes[$key]];
|
||||
}
|
||||
}
|
||||
Redis::connection($this->connection)->setex($this->getModuleRedisPrefix() . "notifications:user:" . $this->identifyUser(), $this->ttl, json_encode($notificationsRedis));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user