/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
api/components/SameFilter.php
90 строк
2 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace api\components; use common\helpers\DateHelper; use common\helpers\GtpHelper; use common\kdf\KApiKey; use common\models\RequestPayload; use Yii; use yii\base\ActionFilter; use yii\db\Exception; use yii\helpers\VarDumper; use yii\mutex\MysqlMutex; /** * Проверка на дублирование запросов * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2022 */ class SameFilter extends ActionFilter { /** * @var null */ private $hash = null; /** * {@inheritdoc} */ public function beforeAction($action) { $is_erp = GtpHelper::isApp(KApiKey::ERP); if ($is_erp) { if (Yii::$app->request->isPost || Yii::$app->request->isPut) { if ($rawBody = Yii::$app->request->rawBody) { $this->hash = md5($rawBody); $mutex = new MysqlMutex(); $mutexName = 'lock:' . $this->hash; try { // удаление записей срок годности которых истёк RequestPayload::deleteAll(['<', 'expired', DateHelper::now()]); $mutex->acquire($mutexName, -1); $rp = new RequestPayload([ 'payload_hash' => $this->hash ]); try { if (!$rp->save()) { Yii::debug(VarDumper::dumpAsString($rp->errors)); throw new Exception(Yii::t('app', 'Error save payload hash')); } } catch (\Exception $e) { throw new ApiException(304, 'Not Modified'); } } finally { $mutex->release($mutexName); } } } } return true; } /** * {@inheritdoc} */ public function afterAction($action, $result) { $parent = parent::afterAction($action, $result); // TODO: Change the autogenerated stub if ($this->hash) { RequestPayload::deleteAll([ 'payload_hash' => $this->hash ]); } return $parent; } }