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 — 0.9 (#389)
by Jérémiah
18:09 queued 14:52
created

AccessResolver::filterResultUsingAccess()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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