Retries::retry()   A
last analyzed

Complexity

Conditions 3
Paths 0

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 0
nop 2
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\ForgePublish\Commands\Traits;
4
5
use Acacha\ForgePublish\Exceptions\TimeoutException;
6
7
/**
8
 * Class Retries.
9
 *
10
 * @package Acacha\ForgePublish\Commands\Traits
11
 */
12
trait Retries
13
{
14
15
    /**
16
     * Retry the callback or fail after x seconds.
17
     *
18
     * @param $timeout
19
     * @param $callback
20
     * @return mixed
21
     * @throws TimeoutException
22
     */
23
    public function retry($timeout, $callback)
24
    {
25
        $start = time();
26
27
        beginning:
28
29
        if ($output = $callback()) {
30
            return $output;
31
        }
32
33
        if (time() - $start < $timeout) {
34
            sleep(5);
35
36
            goto beginning;
37
        }
38
39
        throw new TimeoutException($output);
40
    }
41
}
42