1<?php
2namespace GuzzleHttp\Ring\Future;
3
4use React\Promise\FulfilledPromise;
5use React\Promise\RejectedPromise;
6
7/**
8 * Represents a future value that has been resolved or rejected.
9 */
10class CompletedFutureValue implements FutureInterface
11{
12    protected $result;
13    protected $error;
14
15    private $cachedPromise;
16
17    /**
18     * @param mixed      $result Resolved result
19     * @param \Exception $e      Error. Pass a GuzzleHttp\Ring\Exception\CancelledFutureAccessException
20     *                           to mark the future as cancelled.
21     */
22    public function __construct($result, \Exception $e = null)
23    {
24        $this->result = $result;
25        $this->error = $e;
26    }
27
28    public function wait()
29    {
30        if ($this->error) {
31            throw $this->error;
32        }
33
34        return $this->result;
35    }
36
37    public function cancel() {}
38
39    public function promise()
40    {
41        if (!$this->cachedPromise) {
42            $this->cachedPromise = $this->error
43                ? new RejectedPromise($this->error)
44                : new FulfilledPromise($this->result);
45        }
46
47        return $this->cachedPromise;
48    }
49
50    public function then(
51        callable $onFulfilled = null,
52        callable $onRejected = null,
53        callable $onProgress = null
54    ) {
55        return $this->promise()->then($onFulfilled, $onRejected, $onProgress);
56    }
57}
58