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
Push — master ( 723387...2c62e2 )
by Jérémiah
15:05
created

AccessResolver::extractAdoptedPromise()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Resolver;
6
7
use GraphQL\Executor\Promise\Adapter\SyncPromise;
8
use GraphQL\Executor\Promise\Promise;
9
use GraphQL\Executor\Promise\PromiseAdapter;
10
use GraphQL\Type\Definition\ListOfType;
11
use GraphQL\Type\Definition\ResolveInfo;
12
use Overblog\GraphQLBundle\Error\UserError;
13
use Overblog\GraphQLBundle\Error\UserWarning;
14
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
15
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge;
16
17
class AccessResolver
18
{
19
    /** @var PromiseAdapter */
20
    private $promiseAdapter;
21
22 101
    public function __construct(PromiseAdapter $promiseAdapter)
23
    {
24 101
        $this->promiseAdapter = $promiseAdapter;
25 101
    }
26
27 14
    public function resolve(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [], $useStrictAccess = false)
28
    {
29 14
        if ($useStrictAccess || self::isMutationRootField($resolveArgs[3])) {
30 13
            return $this->checkAccessForStrictMode($accessChecker, $resolveCallback, $resolveArgs);
31
        }
32
33 4
        $resultOrPromise = \call_user_func_array($resolveCallback, $resolveArgs);
34
35 4
        if ($this->isThenable($resultOrPromise)) {
36 1
            return $this->createPromise(
37 1
                $resultOrPromise,
38
                function ($result) use ($accessChecker, $resolveArgs) {
39 1
                    return $this->processFilter($result, $accessChecker, $resolveArgs);
40 1
                }
41
            );
42
        }
43
44 3
        return $this->processFilter($resultOrPromise, $accessChecker, $resolveArgs);
45
    }
46
47 10
    private static function isMutationRootField(ResolveInfo $info): bool
48
    {
49 10
        return 'mutation' === $info->operation->operation && $info->parentType === $info->schema->getMutationType();
50
    }
51
52 13
    private function checkAccessForStrictMode(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [])
53
    {
54 13
        $promiseOrHasAccess = $this->hasAccess($accessChecker, $resolveArgs);
55
        $callback = function ($hasAccess) use ($resolveArgs, $resolveCallback) {
56 13
            if (!$hasAccess) {
57 6
                $exceptionClassName = self::isMutationRootField($resolveArgs[3]) ? UserError::class : UserWarning::class;
58 6
                throw new $exceptionClassName('Access denied to this field.');
59
            }
60
61 7
            return \call_user_func_array($resolveCallback, $resolveArgs);
62 13
        };
63
64 13
        if ($this->isThenable($promiseOrHasAccess)) {
65 2
            return $this->createPromise($promiseOrHasAccess, $callback);
66
        } else {
67 11
            return $callback($promiseOrHasAccess);
68
        }
69
    }
70
71 4
    private function processFilter($result, $accessChecker, $resolveArgs)
72
    {
73
        /** @var ResolveInfo $resolveInfo */
74 4
        $resolveInfo = $resolveArgs[3];
75
76 4
        if (\is_iterable($result) && $resolveInfo->returnType instanceof ListOfType) {
0 ignored issues
show
Bug introduced by
The class GraphQL\Type\Definition\ListOfType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77 1
            foreach ($result as $i => $object) {
78 1
                $result[$i] = $this->hasAccess($accessChecker, $resolveArgs, $object) ? $object : null;
79
            }
80 3
        } elseif ($result instanceof Connection) {
81 1
            $result->edges = \array_map(
82
                function (Edge $edge) use ($accessChecker, $resolveArgs) {
83 1
                    $edge->node = $this->hasAccess($accessChecker, $resolveArgs, $edge->node) ? $edge->node : null;
84
85 1
                    return $edge;
86 1
                },
87 1
                $result->edges
88
            );
89 2
        } elseif (!$this->hasAccess($accessChecker, $resolveArgs, $result)) {
90 1
            throw new UserWarning('Access denied to this field.');
91
        }
92
93 3
        return $result;
94
    }
95
96 14
    private function hasAccess(callable $accessChecker, array $resolveArgs = [], $object = null)
97
    {
98 14
        $resolveArgs[] = $object;
99 14
        $accessOrPromise = \call_user_func_array($accessChecker, $resolveArgs);
100
101 14
        return $accessOrPromise;
102
    }
103
104 14
    private function isThenable($object): bool
105
    {
106 14
        $object = $this->extractAdoptedPromise($object);
107
108 14
        return $this->promiseAdapter->isThenable($object) || $object instanceof SyncPromise;
0 ignored issues
show
Bug introduced by
The class GraphQL\Executor\Promise\Adapter\SyncPromise does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
109
    }
110
111 14
    private function extractAdoptedPromise($object)
112
    {
113 14
        if ($object instanceof Promise) {
0 ignored issues
show
Bug introduced by
The class GraphQL\Executor\Promise\Promise does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
114 3
            $object = $object->adoptedPromise;
115
        }
116
117 14
        return $object;
118
    }
119
120 3
    private function createPromise($promise, callable $onFulfilled = null): Promise
121
    {
122 3
        return $this->promiseAdapter->then(
123 3
            new Promise($this->extractAdoptedPromise($promise), $this->promiseAdapter),
124 3
            $onFulfilled
125
        );
126
    }
127
}
128