1 | <?php declare (strict_types=1); |
||
15 | trait HasWaiterTrait |
||
16 | { |
||
17 | /** |
||
18 | * Provides a blocking operation until the resource has reached a particular state. The method |
||
19 | * will enter a loop, requesting feedback from the remote API until it sends back an appropriate |
||
20 | * status. |
||
21 | * |
||
22 | * @param string $status The state to be reached |
||
23 | * @param int $timeout The maximum timeout. If the total time taken by the waiter has reached |
||
24 | * or exceed this timeout, the blocking operation will immediately cease. |
||
25 | * @param int $sleepPeriod The amount of time to pause between each HTTP request. |
||
26 | */ |
||
27 | public function waitUntil(string $status, $timeout = 60, int $sleepPeriod = 1) |
||
28 | { |
||
29 | $startTime = time(); |
||
30 | |||
31 | while (true) { |
||
32 | $this->retrieve(); |
||
|
|||
33 | |||
34 | if ($this->status == $status || $this->shouldHalt($timeout, $startTime)) { |
||
35 | break; |
||
36 | } |
||
37 | |||
38 | sleep($sleepPeriod); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Provides a blocking operation until the resource has reached a particular state. The method |
||
44 | * will enter a loop, executing the callback until TRUE is returned. This provides great |
||
45 | * flexibility. |
||
46 | * |
||
47 | * @param callable $fn An anonymous function that will be executed on every iteration. You can |
||
48 | * encapsulate your own logic to determine whether the resource has |
||
49 | * successfully transitioned. When TRUE is returned by the callback, |
||
50 | * the loop will end. |
||
51 | * @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached |
||
52 | * or exceed this timeout, the blocking operation will immediately cease. If FALSE |
||
53 | * is provided, the timeout will never be considered. |
||
54 | * @param int $sleepPeriod The amount of time to pause between each HTTP request. |
||
55 | */ |
||
56 | public function waitWithCallback(callable $fn, $timeout = 60, int $sleepPeriod = 1) |
||
57 | { |
||
58 | $startTime = time(); |
||
59 | |||
60 | while (true) { |
||
61 | $this->retrieve(); |
||
62 | |||
63 | $response = call_user_func_array($fn, [$this]); |
||
64 | |||
65 | if ($response === true || $this->shouldHalt($timeout, $startTime)) { |
||
66 | break; |
||
67 | } |
||
68 | |||
69 | sleep($sleepPeriod); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Internal method used to identify whether a timeout has been exceeded. |
||
75 | * |
||
76 | * @param bool|int $timeout |
||
77 | * @param int $startTime |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | private function shouldHalt($timeout, int $startTime) |
||
82 | { |
||
83 | if ($timeout === false) { |
||
84 | return false; |
||
85 | } |
||
86 | |||
87 | return time() - $startTime >= $timeout; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Convenience method providing a blocking operation until the resource transitions to an |
||
92 | * ``ACTIVE`` status. |
||
93 | * |
||
94 | * @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached |
||
95 | * or exceed this timeout, the blocking operation will immediately cease. If FALSE |
||
96 | * is provided, the timeout will never be considered. |
||
97 | */ |
||
98 | public function waitUntilActive($timeout = false) |
||
99 | { |
||
100 | $this->waitUntil('ACTIVE', $timeout); |
||
101 | } |
||
102 | |||
103 | public function waitUntilDeleted($timeout = 60, int $sleepPeriod = 1) |
||
124 | } |
||
125 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.