Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#240)
by Renato
21:21 queued 17:44
created

ReactPromiseAdapter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 68
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B wait() 0 35 5
A isThenable() 0 4 2
A convertThenable() 0 8 2
1
<?php
2
3
namespace Overblog\GraphQLBundle\Executor\Promise\Adapter;
4
5
use GraphQL\Executor\ExecutionResult;
6
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter as BaseReactPromiseAdapter;
7
use GraphQL\Executor\Promise\Promise;
8
use Overblog\GraphQLBundle\Executor\Promise\PromiseAdapterInterface;
9
10
class ReactPromiseAdapter extends BaseReactPromiseAdapter implements PromiseAdapterInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 17
    public function isThenable($value)
16
    {
17 17
        return parent::isThenable($value instanceof Promise ? $value->adoptedPromise : $value);
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 11
    public function convertThenable($thenable)
24
    {
25 11
        if ($thenable instanceof Promise) {
26 1
            return $thenable;
27
        }
28
29 11
        return parent::convertThenable($thenable);
30
    }
31
32
    /**
33
     * Synchronously wait when promise completes.
34
     *
35
     * @param Promise  $promise
36
     * @param callable $onProgress
37
     *
38
     * @return ExecutionResult
39
     *
40
     * @throws \Exception
41
     */
42 17
    public function wait(Promise $promise, callable $onProgress = null)
43
    {
44 17
        if (!$this->isThenable($promise)) {
45 1
            throw new \InvalidArgumentException(sprintf('The "%s" method must be call with compatible a Promise.', __METHOD__));
46
        }
47 16
        $wait = true;
48 16
        $resolvedValue = null;
49 16
        $exception = null;
50
        /** @var \React\Promise\PromiseInterface $reactPromise */
51 16
        $reactPromise = $promise->adoptedPromise;
52
53 15
        $reactPromise->then(function ($values) use (&$resolvedValue, &$wait) {
54 15
            $resolvedValue = $values;
55 15
            $wait = false;
56
        }, function ($reason) use (&$exception, &$wait) {
57 1
            $exception = $reason;
58 1
            $wait = false;
59 16
        });
60
61
        // wait until promise resolution
62
        while ($wait) {
63
            if (null !== $onProgress) {
64
                $onProgress();
65
            }
66
            // less CPU intensive without sacrificing the performance
67
            usleep(5);
68
        }
69
70
        /** @var \Exception|null $exception */
71
        if (null !== $exception) {
72
            throw $exception;
73
        }
74
75
        return $resolvedValue;
76
    }
77
}
78