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