1<?php
2
3namespace React\Promise;
4
5/**
6 * @deprecated 2.8.0 External usage of RejectedPromise is deprecated, use `reject()` instead.
7 */
8class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
9{
10    private $reason;
11
12    public function __construct($reason = null)
13    {
14        if ($reason instanceof PromiseInterface) {
15            throw new \InvalidArgumentException('You cannot create React\Promise\RejectedPromise with a promise. Use React\Promise\reject($promiseOrValue) instead.');
16        }
17
18        $this->reason = $reason;
19    }
20
21    public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
22    {
23        if (null === $onRejected) {
24            return $this;
25        }
26
27        try {
28            return resolve($onRejected($this->reason));
29        } catch (\Throwable $exception) {
30            return new RejectedPromise($exception);
31        } catch (\Exception $exception) {
32            return new RejectedPromise($exception);
33        }
34    }
35
36    public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
37    {
38        if (null === $onRejected) {
39            throw UnhandledRejectionException::resolve($this->reason);
40        }
41
42        $result = $onRejected($this->reason);
43
44        if ($result instanceof self) {
45            throw UnhandledRejectionException::resolve($result->reason);
46        }
47
48        if ($result instanceof ExtendedPromiseInterface) {
49            $result->done();
50        }
51    }
52
53    public function otherwise(callable $onRejected)
54    {
55        if (!_checkTypehint($onRejected, $this->reason)) {
56            return $this;
57        }
58
59        return $this->then(null, $onRejected);
60    }
61
62    public function always(callable $onFulfilledOrRejected)
63    {
64        return $this->then(null, function ($reason) use ($onFulfilledOrRejected) {
65            return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
66                return new RejectedPromise($reason);
67            });
68        });
69    }
70
71    public function progress(callable $onProgress)
72    {
73        return $this;
74    }
75
76    public function cancel()
77    {
78    }
79}
80