| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2018 Google LLC |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions are |
| 9 |
* met: |
| 10 |
* |
| 11 |
* * Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* * Redistributions in binary form must reproduce the above |
| 14 |
* copyright notice, this list of conditions and the following disclaimer |
| 15 |
* in the documentation and/or other materials provided with the |
| 16 |
* distribution. |
| 17 |
* * Neither the name of Google Inc. nor the names of its |
| 18 |
* contributors may be used to endorse or promote products derived from |
| 19 |
* this software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
namespace Dudlewebs\WPMCS\Google\ApiCore; |
| 34 |
|
| 35 |
/** |
| 36 |
* @internal |
| 37 |
*/ |
| 38 |
trait PollingTrait |
| 39 |
{ |
| 40 |
/** |
| 41 |
* @param callable $pollCallable The call to poll. Must return a boolean indicating whether the |
| 42 |
* operation has completed, and the polling loop can be terminated. |
| 43 |
* @param array $options { |
| 44 |
* Options for configuring the polling behaviour. |
| 45 |
* |
| 46 |
* @type int $initialPollDelayMillis The initial polling interval to use, in milliseconds. |
| 47 |
* @type int $pollDelayMultiplier Multiplier applied to the polling interval on each retry. |
| 48 |
* @type int $maxPollDelayMillis The maximum polling interval to use, in milliseconds. |
| 49 |
* @type int $totalPollTimeoutMillis The maximum amount of time to continue polling, in milliseconds. |
| 50 |
* } |
| 51 |
* @return bool |
| 52 |
*/ |
| 53 |
private function poll(callable $pollCallable, array $options) |
| 54 |
{ |
| 55 |
$currentPollDelayMillis = $options['initialPollDelayMillis']; |
| 56 |
$pollDelayMultiplier = $options['pollDelayMultiplier']; |
| 57 |
$maxPollDelayMillis = $options['maxPollDelayMillis']; |
| 58 |
$totalPollTimeoutMillis = $options['totalPollTimeoutMillis']; |
| 59 |
$hasTotalPollTimeout = $totalPollTimeoutMillis > 0.0; |
| 60 |
$endTime = $this->getCurrentTimeMillis() + $totalPollTimeoutMillis; |
| 61 |
while (\true) { |
| 62 |
if ($hasTotalPollTimeout && $this->getCurrentTimeMillis() > $endTime) { |
| 63 |
return \false; |
| 64 |
} |
| 65 |
$this->sleepMillis($currentPollDelayMillis); |
| 66 |
if ($pollCallable()) { |
| 67 |
return \true; |
| 68 |
} |
| 69 |
$currentPollDelayMillis = (int) min([$currentPollDelayMillis * $pollDelayMultiplier, $maxPollDelayMillis]); |
| 70 |
} |
| 71 |
} |
| 72 |
/** |
| 73 |
* Protected to allow overriding for tests |
| 74 |
* |
| 75 |
* @return float Current time in milliseconds |
| 76 |
*/ |
| 77 |
protected function getCurrentTimeMillis() |
| 78 |
{ |
| 79 |
return microtime(\true) * 1000.0; |
| 80 |
} |
| 81 |
/** |
| 82 |
* Protected to allow overriding for tests |
| 83 |
* |
| 84 |
* @param int $millis |
| 85 |
*/ |
| 86 |
protected function sleepMillis(int $millis) |
| 87 |
{ |
| 88 |
usleep($millis * 1000); |
| 89 |
} |
| 90 |
} |
| 91 |
|