Skip to content

Instantly share code, notes, and snippets.

@TheFrozenFire
Created December 30, 2013 19:00
Show Gist options
  • Save TheFrozenFire/8186351 to your computer and use it in GitHub Desktop.
Save TheFrozenFire/8186351 to your computer and use it in GitHub Desktop.
<?php
class RetryManager
{
protected $callback;
protected $retries = 0;
protected $caughtExceptions = array();
public function execute()
{
$callback = $this->getCallback();
if(!is_callable($callback)) {
throw new InvalidArgumentException('No callback specified');
}
for($i = 0; $i < $this->getRetries(); $i++) {
try {
$result = $callback();
} catch (Exception $e) {
if(in_array(get_class($e), $this->getCaughtExceptions())) {
continue;
} else {
throw $e;
}
}
}
return $result;
}
public function getCallback() {
return $this->callback;
}
public function setCallback($callback) {
$this->callback = $callback;
return $this;
}
public function getRetries() {
return $this->retries;
}
public function setRetries($retries) {
$this->retries = $retries;
return $this;
}
public function getCaughtExceptions() {
return $this->caughtExceptions;
}
public function setCaughtExceptions($caughtExceptions) {
$this->caughtExceptions = $caughtExceptions;
return $this;
}
public function addCaughtException($caughtException) {
$this->caughtExceptions[] = $caughtException;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment