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
Pull Request — master (#325)
by Jérémiah
13:59
created

AclConfigProcessor::acl()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 14.432

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 7
cts 15
cp 0.4667
rs 6.7272
cc 7
eloc 14
nc 5
nop 3
crap 14.432
1
<?php
2
3
namespace Overblog\GraphQLBundle\Definition\ConfigProcessor;
4
5
use GraphQL\Type\Definition\ResolveInfo;
6
use Overblog\GraphQLBundle\Definition\LazyConfig;
7
use Overblog\GraphQLBundle\Error\UserWarning;
8
use Overblog\GraphQLBundle\Resolver\AccessResolver;
9
10
final class AclConfigProcessor implements ConfigProcessorInterface
11
{
12
    /** @var AccessResolver */
13
    private $accessResolver;
14
15
    /** @var callable */
16
    private $defaultResolver;
17
18 39
    public function __construct(AccessResolver $accessResolver, callable $defaultResolver)
19
    {
20 39
        $this->accessResolver = $accessResolver;
21 39
        $this->defaultResolver = $defaultResolver;
22 39
    }
23
24
    public static function acl(array $fields, AccessResolver $accessResolver, callable $defaultResolver)
25
    {
26 34
        $deniedAccess = static function () {
27
            throw new UserWarning('Access denied to this field.');
28 34
        };
29 34
        foreach ($fields as &$field) {
30 34
            if (isset($field['access']) && true !== $field['access']) {
31
                $accessChecker = $field['access'];
32
                if (false === $accessChecker) {
33
                    $field['resolve'] = $deniedAccess;
34
                } elseif (is_callable($accessChecker)) {
35 34
                    $field['resolve'] = function ($value, $args, $context, ResolveInfo $info) use ($field, $accessChecker, $accessResolver, $defaultResolver) {
36
                        $resolverCallback = self::findFieldResolver($field, $info, $defaultResolver);
37
                        $isMutation = 'mutation' === $info->operation->operation && $info->parentType === $info->schema->getMutationType();
38
39
                        return $accessResolver->resolve($accessChecker, $resolverCallback, [$value, $args, $context, $info], $isMutation);
40 34
                    };
41
                }
42
            }
43
        }
44
45 34
        return $fields;
46
    }
47
48 View Code Duplication
    public function process(LazyConfig $lazyConfig)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50 39
        $lazyConfig->addPostLoader(function ($config) {
51 39
            if (isset($config['fields']) && is_callable($config['fields'])) {
52 34
                $config['fields'] = function () use ($config) {
53 34
                    $fields = $config['fields']();
54
55 34
                    return static::acl($fields, $this->accessResolver, $this->defaultResolver);
56
                };
57
            }
58
59 39
            return $config;
60 39
        });
61
62 39
        return $lazyConfig;
63
    }
64
65
    /**
66
     * @param array       $field
67
     * @param ResolveInfo $info
68
     * @param callable    $defaultResolver
69
     *
70
     * @return callable
71
     */
72
    private static function findFieldResolver(array $field, ResolveInfo $info, callable $defaultResolver)
73
    {
74
        if (isset($field['resolve'])) {
75
            $resolver = $field['resolve'];
76
        } elseif (isset($info->parentType->config['resolveField'])) {
77
            $resolver = $info->parentType->config['resolveField'];
78
        } else {
79
            $resolver = $defaultResolver;
80
        }
81
82
        return $resolver;
83
    }
84
}
85