26 private const MAX_EXPECTED_TIME = 75;
28 private int $generationId;
31 private ?
string $hash =
null;
32 private ?
array $result =
null;
34 private bool $isDeleted =
false;
35 private ?
int $stepRelationId;
39 private Type\RequestStatus $status = Type\RequestStatus::New;
44 $this->generationId = $generationId;
45 $this->stepId = $stepId;
57 if (!Generation::checkExists($this->generationId))
62 if ($this->status->value >=
Type\RequestStatus::Sent->value)
67 $result = $connector->request($prompt);
68 if (!$result->isSuccess())
70 $this->processError($result->getError());
73 $this->status = Type\RequestStatus::Sent;
74 $data = $result->getData();
79 $this->hash =
$data[
'hash'];
88 if (is_string(
$data[
'result']))
109 $error->message .=
': ' .
$data[
'error'];
129 $errorText = $this->getRequestLimiter()->getTextFromError(
$error);
134 'errorText' => $errorText,
148 private function getRequestLimiter(): RequestLimiter
150 if (empty($this->requestLimiter))
152 $this->requestLimiter =
new RequestLimiter();
155 return $this->requestLimiter;
160 if ($this->status->value <
Type\RequestStatus::Received->value)
165 if (!isset($this->stepRelationId))
170 if ($this->status ===
Type\RequestStatus::Applied)
175 $res = RequestToStepTable::update($this->stepRelationId, [
178 if (!
$res->isSuccess())
183 $this->status = Type\RequestStatus::Applied;
185 return $this->save();
195 if ($this->status !==
Type\RequestStatus::Sent)
200 $this->result = $result;
201 $this->status = Type\RequestStatus::Received;
202 $this->dateReceive =
new DateTime();
204 return $this->save();
214 if ($this->status >
Type\RequestStatus::Sent)
219 $this->error = $error;
220 $this->status = Type\RequestStatus::Received;
221 $this->dateReceive =
new DateTime();
223 return $this->save();
228 $this->isDeleted =
true;
232 private function save(): bool
234 if ($this->status->value <
Type\RequestStatus::Sent->value)
240 'GENERATION_ID' => $this->generationId,
243 'ERROR' => $this->error?->toArray(),
244 'DELETED' => $this->isDeleted,
247 if (isset($this->dateReceive))
249 $fields[
'DATE_RECEIVE'] = $this->dateReceive;
252 if (isset($this->
id) && $this->
id)
255 if (!
$res->isSuccess())
263 if (!
$res->isSuccess())
267 $this->
id =
$res->getId();
270 if (!isset($this->stepRelationId))
272 $res = RequestToStepTable::add([
273 'REQUEST_ID' => $this->
id,
274 'GENERATION_ID' => $this->generationId,
275 'STEP' => $this->stepId,
277 if (!
$res->isSuccess())
282 $this->stepRelationId =
$res->getId();
294 return $this->generationId;
321 return $this->status === Type\RequestStatus::Received;
330 return $this->status === Type\RequestStatus::Applied;
351 ->where(
'GENERATION_ID',
'=', $generationId)
352 ->where(
'STEP_REF.STEP',
'=', $stepId)
353 ->where(
'DELETED',
'=',
'N')
356 return self::getExists(
$filter);
363 ->where(
'HASH',
'=', $hash)
364 ->where(
'DELETED',
'=',
'N')
366 $exists = self::getExists(
$filter);
368 return array_shift($exists);
371 public static function getById(
int $id): ?self
375 ->where(
'ID',
'=', $id)
376 ->where(
'DELETED',
'=',
'N')
378 $exists = self::getExists(
$filter);
380 return array_shift($exists);
390 $res = RequestsTable::query()
400 'STEP' =>
'STEP_REF.STEP',
409 || !
$entity->getStepRef()->getStep()
417 $entity->getStepRef()->getStep(),
434 private function initByEntity(EO_Requests
$request): self
436 $this->
id = $request->getId();
437 $this->isDeleted =
$request->getDeleted();
443 $this->status = Type\RequestStatus::Sent;
450 $this->result = $result;
451 $this->status = Type\RequestStatus::Received;
455 $this->error = Generation\Error::fromArray($error);
456 $this->status = Type\RequestStatus::Received;
459 $this->dateCreate =
$request->getDateCreate();
460 $dateReceive =
$request->getDateReceive();
463 $this->dateReceive = $dateReceive;
469 $step->fillApplied();
470 $this->stepRelationId = $step->getId();
471 if (!empty($this->result) && $step->getApplied())
473 $this->status = Generation\Type\RequestStatus::Applied;
480 private function isTimeIsOver(): bool
483 isset($this->dateReceive)
484 || isset($this->result)
485 || isset($this->error)
491 return ((
new \DateTime())->
getTimestamp() - $this->dateCreate->getTimestamp()) > self::MAX_EXPECTED_TIME;
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)