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

Passed
Push — 0.11 ( 38715f...dd1177 )
by Jérémiah
16:39
created

AccessResolver::filterResultUsingAccess()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4
1
<?php
2
3
namespace Overblog\GraphQLBundle\Resolver;
4
5
use GraphQL\Executor\Promise\Adapter\SyncPromise;
6
use GraphQL\Executor\Promise\Promise;
7
use GraphQL\Executor\Promise\PromiseAdapter;
8
use GraphQL\Type\Definition\ListOfType;
9
use GraphQL\Type\Definition\ResolveInfo;
10
use Overblog\GraphQLBundle\Error\UserError;
11
use Overblog\GraphQLBundle\Error\UserWarning;
12
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
13
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge;
14
15
class AccessResolver
16
{
17
    /** @var PromiseAdapter */
18
    private $promiseAdapter;
19
20 90
    public function __construct(PromiseAdapter $promiseAdapter)
21
    {
22 90
        $this->promiseAdapter = $promiseAdapter;
23 90
    }
24
25 11
    public function resolve(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [], $isMutation = false)
26
    {
27
        // operation is mutation and is mutation field
28 11
        if ($isMutation) {
29 3
            if (!$this->hasAccess($accessChecker, null, $resolveArgs)) {
30 1
                throw new UserError('Access denied to this field.');
31
            }
32
33 2
            $result = \call_user_func_array($resolveCallback, $resolveArgs);
34
        } else {
35 10
            $result = $this->filterResultUsingAccess($accessChecker, $resolveCallback, $resolveArgs);
36
        }
37
38 8
        return $result;
39
    }
40
41 10
    private function filterResultUsingAccess(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [])
42
    {
43 10
        $result = \call_user_func_array($resolveCallback, $resolveArgs);
44 10
        if ($result instanceof Promise) {
45 1
            $result = $result->adoptedPromise;
46
        }
47
48 10
        if ($this->promiseAdapter->isThenable($result) || $result instanceof SyncPromise) {
49 1
            return $this->promiseAdapter->then(
50 1
                new Promise($result, $this->promiseAdapter),
51
                function ($result) use ($accessChecker, $resolveArgs) {
52 1
                    return $this->processFilter($result, $accessChecker, $resolveArgs);
53 1
                }
54
            );
55
        }
56
57 10
        return $this->processFilter($result, $accessChecker, $resolveArgs);
58
    }
59
60 10
    private function processFilter($result, $accessChecker, $resolveArgs)
61
    {
62
        /** @var ResolveInfo $resolveInfo */
63 10
        $resolveInfo = $resolveArgs[3];
64
65 10
        if (self::isIterable($result) && $resolveInfo->returnType instanceof ListOfType) {
66 3
            foreach ($result as $i => $object) {
67 3
                $result[$i] = $this->hasAccess($accessChecker, $object, $resolveArgs) ? $object : null;
68
            }
69 7
        } elseif ($result instanceof Connection) {
70 1
            $result->edges = \array_map(
71
                function (Edge $edge) use ($accessChecker, $resolveArgs) {
72 1
                    $edge->node = $this->hasAccess($accessChecker, $edge->node, $resolveArgs) ? $edge->node : null;
73
74 1
                    return $edge;
75 1
                },
76 1
                $result->edges
77
            );
78 7
        } elseif (!$this->hasAccess($accessChecker, $result, $resolveArgs)) {
79 3
            throw new UserWarning('Access denied to this field.');
80
        }
81
82 7
        return $result;
83
    }
84
85 11
    private function hasAccess(callable $accessChecker, $object, array $resolveArgs = [])
86
    {
87 11
        $resolveArgs[] = $object;
88 11
        $access = (bool) \call_user_func_array($accessChecker, $resolveArgs);
89
90 11
        return $access;
91
    }
92
93
    /**
94
     * @param mixed $data
95
     *
96
     * @return bool
97
     */
98 10
    private static function isIterable($data)
99
    {
100 10
        if (\function_exists('is_iterable')) {
101 10
            return \is_iterable($data);
102
        } else {
103
            return \is_array($data) || (\is_object($data) && ($data instanceof \Traversable));
104
        }
105
    }
106
}
107