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

AccessResolver::filterResultUsingAccess()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 30
ccs 23
cts 23
cp 1
rs 8.439
cc 5
eloc 20
nc 3
nop 3
crap 5
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 Overblog\GraphQLBundle\Error\UserWarning;
15
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
16
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge;
17
18
class AccessResolver
19
{
20 10
    public function resolve(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [], $isMutation = false)
21
    {
22
        // operation is mutation and is mutation field
23 10
        if ($isMutation) {
24 3
            $result = $this->checkAccess($accessChecker, null, $resolveArgs, true) ? call_user_func_array($resolveCallback, $resolveArgs) : null;
25 2
        } else {
26 9
            $result = $this->filterResultUsingAccess($accessChecker, $resolveCallback, $resolveArgs);
27
        }
28
29 7
        return $result;
30
    }
31
32 9
    private function filterResultUsingAccess(callable $accessChecker, callable $resolveCallback, array $resolveArgs = [])
33
    {
34 9
        $result = call_user_func_array($resolveCallback, $resolveArgs);
35
36 9
        switch (true) {
37 9
            case is_array($result):
38 3
                $result = array_map(
39
                    function ($object) use ($accessChecker, $resolveArgs) {
40 3
                        return $this->checkAccess($accessChecker, $object, $resolveArgs) ? $object : null;
41 3
                    },
42
                    $result
43 3
                );
44 3
                break;
45 6
            case $result instanceof Connection:
46 1
                $result->edges = array_map(
47 1
                    function (Edge $edge) use ($accessChecker, $resolveArgs) {
48 1
                        $edge->node = $this->checkAccess($accessChecker, $edge->node, $resolveArgs) ? $edge->node : null;
49
50 1
                        return $edge;
51 1
                    },
52 1
                    $result->edges
53 1
                );
54 1
                break;
55 6
            default:
56 6
                $this->checkAccess($accessChecker, $result, $resolveArgs, true);
57 3
                break;
58 6
        }
59
60 6
        return $result;
61
    }
62
63 10
    private function checkAccess(callable $accessChecker, $object, array $resolveArgs = [], $throwException = false)
64
    {
65 10
        $resolveArgs[] = $object;
66
67
        try {
68 10
            $access = (bool) call_user_func_array($accessChecker, $resolveArgs);
69 10
        } catch (\Exception $e) {
70 1
            $access = false;
71
        }
72 10
        if ($throwException && !$access) {
73 4
            throw new UserWarning('Access denied to this field.');
74
        }
75
76 7
        return $access;
77
    }
78
}
79