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 (#73)
by Jérémiah
05:28
created

AccessResolver::filterResultUsingAccess()   C

Complexity

Conditions 8
Paths 3

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 25
cts 25
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 22
nc 3
nop 3
crap 8
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\Promise;
15
use GraphQL\Executor\Promise\PromiseAdapter as PromiseAdapterInterface;
16
use Overblog\GraphQLBundle\Error\UserError;
17
use Overblog\GraphQLBundle\Error\UserWarning;
18
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
19
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge;
20
21
class AccessResolver
22
{
23
    /** @var PromiseAdapterInterface */
24
    private $promiseAdapter;
25
26 1
    public function __construct(PromiseAdapterInterface $promiseAdapter)
27
    {
28 1
        $this->promiseAdapter = $promiseAdapter;
29 1
    }
30
31 9
    public function resolve(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [], $isMutation = false)
32
    {
33
        // operation is mutation and is mutation field
34 9
        if ($isMutation) {
35 3
            if (!$this->hasAccess($accessChecker, null, $resolveArgs)) {
36 1
                throw new UserError('Access denied to this field.');
37
            }
38
39 2
            $result = call_user_func_array($resolveCallback, $resolveArgs);
40 2
        } else {
41 8
            $result = $this->filterResultUsingAccess($accessChecker, $resolveCallback, $resolveArgs);
42
        }
43
44 7
        return $result;
45
    }
46
47 8
    private function filterResultUsingAccess(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [])
48
    {
49 8
        $result = call_user_func_array($resolveCallback, $resolveArgs);
50
51
        $onFulFilled = function ($result) use ($accessChecker, $resolveArgs) {
52 8
            if (is_array($result)) {
53 3
                $result = array_map(
54
                        function ($object) use ($accessChecker, $resolveArgs) {
55 3
                            return $this->hasAccess($accessChecker, $object, $resolveArgs) ? $object : null;
56 3
                        },
57
                        $result
58 3
                    );
59 8
            } elseif ($result instanceof Connection) {
60 1
                $result->edges = array_map(
61 1
                        function (Edge $edge) use ($accessChecker, $resolveArgs) {
62 1
                            $edge->node = $this->hasAccess($accessChecker, $edge->node, $resolveArgs) ? $edge->node : null;
63
64 1
                            return $edge;
65 1
                        },
66 1
                        $result->edges
67 1
                    );
68 5
            } elseif (!$this->hasAccess($accessChecker, $result, $resolveArgs)) {
69 2
                throw new UserWarning('Access denied to this field.');
70
            }
71
72 6
            return $result;
73 8
        };
74
75 8
        if ($this->promiseAdapter->isThenable($result)) {
76 1
            if (!$result instanceof Promise) {
77 1
                $result = new Promise($result, $this->promiseAdapter);
78 1
            }
79
80 1
            return $this->promiseAdapter->then($result, $onFulFilled);
81
        }
82
83 8
        return $onFulFilled($result);
84
    }
85
86 9
    private function hasAccess(callable $accessChecker, $object, array $resolveArgs = [])
87
    {
88 9
        $resolveArgs[] = $object;
89 9
        $access = (bool) call_user_func_array($accessChecker, $resolveArgs);
90
91 9
        return $access;
92
    }
93
}
94