Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] TYPO3 12: Use php Enum for event type and status #49

Open
3l73 opened this issue Oct 4, 2024 · 0 comments
Open

[Improvement] TYPO3 12: Use php Enum for event type and status #49

3l73 opened this issue Oct 4, 2024 · 0 comments

Comments

@3l73
Copy link

3l73 commented Oct 4, 2024

The status for the events inside the queue are currently coded at multiple places.

Since PHP 8 it is possible to use enums, which is a easy way to define these status

enum EventState {
    case New;
    case Pending;
    case Claimed;
    case Failed;
    case Deferred;
    case All;
    
    public function toLower(): string
    {
        return strtolower($this->name);
    }
}

The value of the enum could be accessed via name and be used as values for the database:

  protected function queueEvent(Module $module, array $result): Event
  {
    $event = $this->eventRepository->findOneByModuleAndEventId($module, (int)$result['id']);
    $new = false;
    if (!$event) {
      $event = new Event();
      $new = true;
    } elseif ($event->getStatus() === EventState::Claimed->toLower()) {
      return $event;
    }

    $event->setModule($module);
    $event->setCrdate(strtotime($result['mod_time']));
    $event->setEventId($result['id']);
    $event->setObjectId($result['object_id']);
    $event->setEventType(Event::resolveEventType($result['event_type']));
    $event->setStatus(EventState::Pending->toLower());

Since the enum can be used as argument for methods, this can be used instead of constants.

Example:

printName(EventState::Claimed);

function printName(EventState $state) {
 print $state->name . PHP_EOL;
} 
@3l73 3l73 changed the title [Improvement] TYPO3 12: Use php Enum for event status [Improvement] TYPO3 12: Use php Enum for event type and status Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant