silverstripe /
vendor-plugin
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\VendorPlugin\Methods; |
||
| 4 | |||
| 5 | use RuntimeException; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Attempt a list of methods until failure. |
||
| 9 | * Note: Only treates RuntimeException as retryable, and will fail on all other error |
||
| 10 | */ |
||
| 11 | class ChainedMethod implements ExposeMethod |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var ExposeMethod[] |
||
| 15 | */ |
||
| 16 | protected $failovers = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param ExposeMethod[] ...$failovers List of failovers |
||
| 20 | */ |
||
| 21 | public function __construct(...$failovers) |
||
| 22 | { |
||
| 23 | $this->failovers = $failovers; |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Exposes the directory with the given paths |
||
| 28 | * |
||
| 29 | * @param string $source Full filesystem path to file source |
||
| 30 | * @param string $target Full filesystem path to the target directory |
||
| 31 | * @throws RuntimeException If could not be exposed |
||
| 32 | */ |
||
| 33 | public function exposeDirectory($source, $target) |
||
| 34 | { |
||
| 35 | $lastException = null; |
||
| 36 | foreach ($this->failovers as $failover) { |
||
| 37 | try { |
||
| 38 | $failover->exposeDirectory($source, $target); |
||
| 39 | return; // Return on first success |
||
| 40 | } catch (RuntimeException $lastException) { |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 41 | } |
||
| 42 | } |
||
| 43 | if ($lastException) { |
||
| 44 | throw $lastException; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 |