<?php

namespace Application\Service\Extension\Middleware\components;

use Application\Form\item\patient\PatientFormModel;
use Application\Service\Exceptions\InvalidArgumentException;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Middleware\BaseMiddlewareComponent;

/**
 * @property-read PatientFormModel $owner
 */
class RateLimit extends BaseMiddlewareComponent
{

    function execute()
    {
        $key = $this->owner->itemId . $this->owner->patientId . $this->owner->formId;
        if ($this->owner->scenario == $this->owner::SCENARIO_EXCEL_IMPORT) {
            return;
        }

        if (!Laminas::$serviceManager->redisExtend->getRedisInstance()->set($key, 'value', ['nx', 'ex' => 1])) {
            throw new InvalidArgumentException('请求太频繁啦~');
        }
    }
}