Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active August 26, 2022 18:34
Show Gist options
  • Save mikemix/1ef4eee498609436074df9cb349924bb to your computer and use it in GitHub Desktop.
Save mikemix/1ef4eee498609436074df9cb349924bb to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace App\Notification\Implementation;
use App\Notification\NotificationInterface;
use App\Notification\TransportInterface;
/**
* {@inheritDoc}
*
* @template-implements TransportInterface<TransportInterface>
*/
final class RepeatableTransportDecorator implements TransportInterface
{
private TransportInterface $transport;
private int $retryCount;
public function __construct(TransportInterface $transport, int $retryCount)
{
$this->transport = $transport;
$this->retryCount = $retryCount;
}
public function supports(NotificationInterface $notification): bool
{
return $this->transport->supports($notification);
}
/**
* {@inheritDoc}
*/
public function deliver(NotificationInterface $notification): void
{
$tryCounter = 0;
do {
try {
$this->transport->deliver($notification);
return;
} catch (\Exception $exception) {
++$tryCounter;
}
} while ($tryCounter < $this->retryCount);
throw NotificationSendException::duringDelivery($exception);
}
}
@Myks92
Copy link

Myks92 commented Aug 25, 2022

Fix:

public function deliver(NotificationInterface $notification): void
    {
        $tryCounter = 0;
        
        do {
            try {
                $this->transport->deliver($notification);

                return;
            } catch (\Exception $exception) {
                ++$tryCounter;
            }
        } while ($tryCounter < $this->retryCount);
        
        throw NotificationSendException::duringDelivery($exception);
    }

@mikemix
Copy link
Author

mikemix commented Aug 26, 2022

Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment