acceptAllInputs = $acceptAllInputs; } /** * Add an item to be cleaned up. * * @param mixed $toDelete Unless the class was created with * `$acceptAllInputs = true`, either a callable with no arguments, or * an object with a `delete` method. * @return void * * @experimental * @internal */ public function add($toDelete) { if (!$this->acceptAllInputs) { if (!is_callable($toDelete) && !method_exists($toDelete, 'delete')) { throw new \BadMethodCallException( 'Deletion Queue requires a callable, or an object with a `delete` method.' ); } if (!is_callable($toDelete)) { $toDelete = function () use ($toDelete) { $toDelete->delete(); }; } } $this->queue[] = $toDelete; } /** * Process all items in the deletion queue. * * @return void * * @experimental * @internal */ public function process(callable $action = null) { if ($action) { $action($this->queue); } else { $backoff = new ExponentialBackoff(8); foreach ($this->queue as $item) { $backoff->execute(function () use ($item) { try { call_user_func($item); } catch (NotFoundException $e) { } catch (ApiException $apiException) { if ($apiException->getStatus() !== 'NOT_FOUND') { throw $apiException; } } }); } } } }