Retries   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A retry() 0 18 3
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