Completed
Pull Request — master (#550)
by Jáchym
26:52 queued 22:59
created

SyncPromise::resolve()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8.0052

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 34
ccs 22
cts 23
cp 0.9565
rs 8.4444
c 0
b 0
f 0
cc 8
nc 7
nop 1
crap 8.0052
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Executor\Promise\Adapter;
6
7
use Exception;
8
use GraphQL\Executor\ExecutionResult;
9
use GraphQL\Utils\Utils;
10
use SplQueue;
11
use Throwable;
12
use function is_object;
13
use function method_exists;
14
15
/**
16
 * Simplistic (yet full-featured) implementation of Promises A+ spec for regular PHP `sync` mode
17
 * (using queue to defer promises execution)
18
 */
19
class SyncPromise
20
{
21
    const PENDING   = 'pending';
22
    const FULFILLED = 'fulfilled';
23
    const REJECTED  = 'rejected';
24
25
    /** @var SplQueue */
26
    public static $queue;
27
28
    /** @var string */
29
    public $state = self::PENDING;
30
31
    /** @var ExecutionResult|Throwable */
32
    public $result;
33
34
    /**
35
     * Promises created in `then` method of this promise and awaiting for resolution of this promise
36
     *
37
     * @var mixed[][]
38
     */
39
    private $waiting = [];
40
41 81
    public static function runQueue() : void
42
    {
43 81
        $q = self::$queue;
44 81
        while ($q !== null && ! $q->isEmpty()) {
45 81
            $task = $q->dequeue();
46 81
            $task();
47
        }
48 81
    }
49
50 275
    public function resolve($value) : self
51
    {
52 275
        switch ($this->state) {
53 275
            case self::PENDING:
54 270
                if ($value === $this) {
55 1
                    throw new Exception('Cannot resolve promise with self');
56
                }
57 270
                if (is_object($value) && method_exists($value, 'then')) {
58 24
                    $value->then(
59
                        function ($resolvedValue) {
60 13
                            $this->resolve($resolvedValue);
61 24
                        },
62
                        function ($reason) {
63 13
                            $this->reject($reason);
64 24
                        }
65
                    );
66
67 24
                    return $this;
68
                }
69
70 270
                $this->state  = self::FULFILLED;
71 270
                $this->result = $value;
72 270
                $this->enqueueWaitingPromises();
73 270
                break;
74 15
            case self::FULFILLED:
75 5
                if ($this->result !== $value) {
76 5
                    throw new Exception('Cannot change value of fulfilled promise');
77
                }
78
                break;
79 10
            case self::REJECTED:
80 10
                throw new Exception('Cannot resolve rejected promise');
81
        }
82
83 270
        return $this;
84
    }
85
86 49
    public function reject($reason) : self
87
    {
88 49
        if (! $reason instanceof Throwable) {
89 1
            throw new Exception('SyncPromise::reject() has to be called with an instance of \Throwable');
90
        }
91
92 49
        switch ($this->state) {
93 49
            case self::PENDING:
94 44
                $this->state  = self::REJECTED;
95 44
                $this->result = $reason;
96 44
                $this->enqueueWaitingPromises();
97 44
                break;
98 15
            case self::REJECTED:
99 10
                if ($reason !== $this->result) {
100 10
                    throw new Exception('Cannot change rejection reason');
101
                }
102
                break;
103 5
            case self::FULFILLED:
104 5
                throw new Exception('Cannot reject fulfilled promise');
105
        }
106
107 44
        return $this;
108
    }
109
110 280
    private function enqueueWaitingPromises() : void
111
    {
112 280
        Utils::invariant(
113 280
            $this->state !== self::PENDING,
114 280
            'Cannot enqueue derived promises when parent is still pending'
115
        );
116
117 280
        foreach ($this->waiting as $descriptor) {
118
            self::getQueue()->enqueue(function () use ($descriptor) {
119
                /** @var self $promise */
120 81
                [$promise, $onFulfilled, $onRejected] = $descriptor;
121
122 81
                if ($this->state === self::FULFILLED) {
123
                    try {
124 77
                        $promise->resolve($onFulfilled === null ? $this->result : $onFulfilled($this->result));
125 14
                    } catch (Throwable $e) {
126 77
                        $promise->reject($e);
127
                    }
128 34
                } elseif ($this->state === self::REJECTED) {
129
                    try {
130 34
                        if ($onRejected === null) {
131 23
                            $promise->reject($this->result);
132
                        } else {
133 34
                            $promise->resolve($onRejected($this->result));
134
                        }
135 14
                    } catch (Throwable $e) {
136 14
                        $promise->reject($e);
137
                    }
138
                }
139 81
            });
140
        }
141 280
        $this->waiting = [];
142 280
    }
143
144 254
    public static function getQueue() : SplQueue
145
    {
146 254
        return self::$queue ?: self::$queue = new SplQueue();
147
    }
148
149 82
    public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
150
    {
151 82
        if ($this->state === self::REJECTED && $onRejected === null) {
152 5
            return $this;
153
        }
154 82
        if ($this->state === self::FULFILLED && $onFulfilled === null) {
155 5
            return $this;
156
        }
157 82
        $tmp             = new self();
158 82
        $this->waiting[] = [$tmp, $onFulfilled, $onRejected];
159
160 82
        if ($this->state !== self::PENDING) {
161 57
            $this->enqueueWaitingPromises();
162
        }
163
164 82
        return $tmp;
165
    }
166
}
167