|
8 | 8 | */
|
9 | 9 | class MailtrapWait
|
10 | 10 | {
|
11 | | -/** |
12 | | - * @var Mailtrap |
13 | | - */ |
14 | | -protected $mailtrap; |
15 | | -/** |
16 | | - * @var int |
17 | | - */ |
18 | | -protected $timeout; |
19 | | -/** |
20 | | - * @var int |
21 | | - */ |
22 | | -protected $interval; |
| 11 | +/** |
| 12 | + * @var Mailtrap |
| 13 | + */ |
| 14 | +protected $mailtrap; |
| 15 | +/** |
| 16 | + * @var int |
| 17 | + */ |
| 18 | +protected $timeout; |
| 19 | +/** |
| 20 | + * @var int |
| 21 | + */ |
| 22 | +protected $interval; |
23 | 23 |
|
24 | | -/** |
25 | | - * MailtrapWait constructor. |
26 | | - * |
27 | | - * @param Mailtrap $mailtrap |
28 | | - * @param null|int $timeout_in_second |
29 | | - * @param null|int $interval_in_millisecond |
30 | | - */ |
31 | | -public function __construct(Mailtrap $mailtrap, $timeout_in_second = null, $interval_in_millisecond = null) |
32 | | -{ |
33 | | -$this->mailtrap = $mailtrap; |
34 | | -$this->timeout = isset($timeout_in_second) ? $timeout_in_second : 30; |
35 | | -$this->interval = $interval_in_millisecond ?: 250; |
36 | | -} |
| 24 | +/** |
| 25 | + * MailtrapWait constructor. |
| 26 | + * |
| 27 | + * @param Mailtrap $mailtrap |
| 28 | + * @param null|int $timeout_in_second |
| 29 | + * @param null|int $interval_in_millisecond |
| 30 | + */ |
| 31 | +public function __construct(Mailtrap $mailtrap, $timeout_in_second = null, $interval_in_millisecond = null) |
| 32 | +{ |
| 33 | +$this->mailtrap = $mailtrap; |
| 34 | +$this->timeout = isset($timeout_in_second) ? $timeout_in_second : 30; |
| 35 | +$this->interval = $interval_in_millisecond ?: 250; |
| 36 | +} |
37 | 37 |
|
38 | | -/** |
39 | | - * Calls the function provided with the driver as an argument until the return value is not falsey. |
40 | | - * |
41 | | - * @param callable $function |
42 | | -* @param string $message |
43 | | - * |
44 | | - * @throws \Exception |
45 | | - * @return mixed The return value of $function |
46 | | - */ |
47 | | -public function until($function, $message = '') |
48 | | -{ |
49 | | -$end = microtime(true) + $this->timeout; |
50 | | -$last_exception = null; |
| 38 | +/** |
| 39 | + * Calls the function provided with the driver as an argument until the return value is not falsey. |
| 40 | + * |
| 41 | + * @param callable $function |
| 42 | +* @param string $message |
| 43 | + * |
| 44 | + * @throws \Exception |
| 45 | + * @return mixed The return value of $function |
| 46 | + */ |
| 47 | +public function until($function, $message = '') |
| 48 | +{ |
| 49 | +$end = microtime(true) + $this->timeout; |
| 50 | +$last_exception = null; |
51 | 51 |
|
52 | | -while ($end > microtime(true ) ) { |
53 | | -try { |
54 | | -$ret_val = call_user_func($function, $this->mailtrap); |
55 | | -if ($ret_val) { |
56 | | -return $ret_val; |
57 | | -} |
58 | | -} catch (\Exception $e) { |
59 | | -$last_exception = $e; |
60 | | -} |
61 | | -usleep($this->interval * 1000); |
62 | | -} |
| 52 | +while ($end > microtime(true)) { |
| 53 | +try { |
| 54 | +$ret_val = call_user_func($function, $this->mailtrap); |
| 55 | +if ($ret_val) { |
| 56 | +return $ret_val; |
| 57 | +} |
| 58 | +} catch (\Exception $e) { |
| 59 | +$last_exception = $e; |
| 60 | +} |
| 61 | +usleep($this->interval * 1000); |
| 62 | +} |
63 | 63 |
|
64 | | -if ($last_exception) { |
65 | | -throw $last_exception; |
66 | | -} |
| 64 | +if ($last_exception) { |
| 65 | +throw $last_exception; |
| 66 | +} |
67 | 67 |
|
68 | | -throw new \Exception($message); |
69 | | -} |
| 68 | +throw new \Exception($message); |
| 69 | +} |
70 | 70 | }
|
0 commit comments