*/ private array $items = []; /** * Next id. * * @var int */ private int $next_id = 1; /** * {@inheritDoc} * * @param int $id Id. */ public function find_by_id( int $id ): ?Automation { return $this->items[ $id ] ?? null; } /** * {@inheritDoc} * * @param Uuid $uuid UUID. */ public function find_by_uuid( Uuid $uuid ): ?Automation { foreach ( $this->items as $item ) { if ( $item->uuid()->equals( $uuid ) ) { return $item; } } return null; } /** * {@inheritDoc} * * @param string $recipe_key Recipe key. */ public function find_by_recipe_key( string $recipe_key ): ?Automation { foreach ( $this->items as $item ) { if ( $item->recipe_key() === $recipe_key ) { return $item; } } return null; } /** * {@inheritDoc} * * @param string $event Event. * * @return list */ public function find_enabled_by_event( string $event ): array { $out = []; foreach ( $this->items as $item ) { if ( $item->is_enabled() && $item->trigger()->event() === $event ) { $out[] = $item; } } return $out; } /** * {@inheritDoc} * * @return list */ public function all(): array { return array_values( $this->items ); } /** * {@inheritDoc} * * @param Automation $automation Entity. */ public function save( Automation $automation ): void { if ( null === $automation->id() ) { $automation->assign_id( $this->next_id++ ); } $this->items[ (int) $automation->id() ] = $automation; } /** * {@inheritDoc} * * @param Automation $automation Entity. */ public function delete( Automation $automation ): void { $id = $automation->id(); if ( null !== $id ) { unset( $this->items[ $id ] ); } } }