summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php
diff options
context:
space:
mode:
authorLoic GUEGAN <loic@Manzerbredes.home>2016-04-16 19:10:27 +0200
committerLoic GUEGAN <loic@Manzerbredes.home>2016-04-16 19:10:27 +0200
commita3ff4d243e2ac37d4516ae56ff86985eadc00eb8 (patch)
tree32e71c42855cc46d95d9c6b74ad2c145eb9ec7eb /server/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php
parent396c90f921c30de2d15d2ce52d5d1beabf8eb52d (diff)
parent31d2d0c158ad4daa3dde7a905f3c2e312c194f2e (diff)
Test
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php')
-rwxr-xr-xserver/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php124
1 files changed, 0 insertions, 124 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php b/server/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php
deleted file mode 100755
index ddc4bef..0000000
--- a/server/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php
+++ /dev/null
@@ -1,124 +0,0 @@
-<?php declare(strict_types=1);
-
-namespace OpenCloud\Common\Resource;
-
-use OpenCloud\Common\Error\BadResponseError;
-
-/**
- * Contains reusable functionality for resources that have long operations which require waiting in
- * order to reach a particular state.
- *
- * @codeCoverageIgnore
- *
- * @package OpenCloud\Common\Resource
- */
-trait HasWaiterTrait
-{
- /**
- * Provides a blocking operation until the resource has reached a particular state. The method
- * will enter a loop, requesting feedback from the remote API until it sends back an appropriate
- * status.
- *
- * @param string $status The state to be reached
- * @param int $timeout The maximum timeout. If the total time taken by the waiter has reached
- * or exceed this timeout, the blocking operation will immediately cease.
- * @param int $sleepPeriod The amount of time to pause between each HTTP request.
- */
- public function waitUntil(string $status, $timeout = 60, int $sleepPeriod = 1)
- {
- $startTime = time();
-
- while (true) {
- $this->retrieve();
-
- if ($this->status == $status || $this->shouldHalt($timeout, $startTime)) {
- break;
- }
-
- sleep($sleepPeriod);
- }
- }
-
- /**
- * Provides a blocking operation until the resource has reached a particular state. The method
- * will enter a loop, executing the callback until TRUE is returned. This provides great
- * flexibility.
- *
- * @param callable $fn An anonymous function that will be executed on every iteration. You can
- * encapsulate your own logic to determine whether the resource has
- * successfully transitioned. When TRUE is returned by the callback,
- * the loop will end.
- * @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached
- * or exceed this timeout, the blocking operation will immediately cease. If FALSE
- * is provided, the timeout will never be considered.
- * @param int $sleepPeriod The amount of time to pause between each HTTP request.
- */
- public function waitWithCallback(callable $fn, $timeout = 60, int $sleepPeriod = 1)
- {
- $startTime = time();
-
- while (true) {
- $this->retrieve();
-
- $response = call_user_func_array($fn, [$this]);
-
- if ($response === true || $this->shouldHalt($timeout, $startTime)) {
- break;
- }
-
- sleep($sleepPeriod);
- }
- }
-
- /**
- * Internal method used to identify whether a timeout has been exceeded.
- *
- * @param bool|int $timeout
- * @param int $startTime
- *
- * @return bool
- */
- private function shouldHalt($timeout, int $startTime)
- {
- if ($timeout === false) {
- return false;
- }
-
- return time() - $startTime >= $timeout;
- }
-
- /**
- * Convenience method providing a blocking operation until the resource transitions to an
- * ``ACTIVE`` status.
- *
- * @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached
- * or exceed this timeout, the blocking operation will immediately cease. If FALSE
- * is provided, the timeout will never be considered.
- */
- public function waitUntilActive($timeout = false)
- {
- $this->waitUntil('ACTIVE', $timeout);
- }
-
- public function waitUntilDeleted($timeout = 60, int $sleepPeriod = 1)
- {
- $startTime = time();
-
- while (true) {
- try {
- $this->retrieve();
- } catch (BadResponseError $e) {
- if ($e->getResponse()->getStatusCode() === 404) {
- break;
- }
- throw $e;
- }
-
- if ($this->shouldHalt($timeout, $startTime)) {
- break;
- }
-
- sleep($sleepPeriod);
- }
- }
-}