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 (#277)
by Jérémiah
20:41
created

AclConfigProcessor::acl()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 5
nop 3
1
<?php
2
3
namespace Overblog\GraphQLBundle\Definition\ConfigProcessor;
4
5
use GraphQL\Type\Definition\ResolveInfo;
6
use Overblog\GraphQLBundle\Definition\Argument;
7
use Overblog\GraphQLBundle\Definition\LazyConfig;
8
use Overblog\GraphQLBundle\Error\UserWarning;
9
use Overblog\GraphQLBundle\Resolver\AccessResolver;
10
11
final class AclConfigProcessor implements ConfigProcessorInterface
12
{
13
    /** @var AccessResolver */
14
    private $accessResolver;
15
16
    /** @var callable */
17
    private $defaultResolver;
18
19
    public function __construct(AccessResolver $accessResolver, callable $defaultResolver)
20
    {
21
        $this->accessResolver = $accessResolver;
22
        $this->defaultResolver = $defaultResolver;
23
    }
24
25
    public static function acl(array $fields, AccessResolver $accessResolver, callable $defaultResolver)
26
    {
27
        $deniedAccess = static function () {
28
            throw new UserWarning('Access denied to this field.');
29
        };
30
        foreach ($fields as &$field) {
31
            if (isset($field['access']) && true !== $field['access']) {
32
                $accessChecker = $field['access'];
33
                if (false === $accessChecker) {
34
                    $field['resolve'] = $deniedAccess;
35
                } elseif (is_callable($accessChecker)) {
36
                    $field['resolve'] = function ($value, $args, $context, ResolveInfo $info) use ($field, $accessChecker, $accessResolver, $defaultResolver) {
37
                        $resolverCallback = self::findFieldResolver($field, $info, $defaultResolver);
38
                        $isMutation = 'mutation' === $info->operation->operation && $info->parentType === $info->schema->getMutationType();
39
40
                        return $accessResolver->resolve($accessChecker, $resolverCallback, [$value, new Argument($args), $context, $info], $isMutation);
41
                    };
42
                }
43
            }
44
        }
45
46
        return $fields;
47
    }
48
49 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...
50
    {
51
        $lazyConfig->addPostLoader(function ($config) {
52
            if (isset($config['fields']) && is_callable($config['fields'])) {
53
                $config['fields'] = function () use ($config) {
54
                    $fields = $config['fields']();
55
56
                    return static::acl($fields, $this->accessResolver, $this->defaultResolver);
57
                };
58
            }
59
60
            return $config;
61
        });
62
63
        return $lazyConfig;
64
    }
65
66
    /**
67
     * @param array       $field
68
     * @param ResolveInfo $info
69
     * @param callable    $defaultResolver
70
     *
71
     * @return callable
72
     */
73
    private static function findFieldResolver(array $field, ResolveInfo $info, callable $defaultResolver)
74
    {
75
        if (isset($field['resolve'])) {
76
            $resolver = $field['resolve'];
77
        } elseif (isset($info->parentType->config['resolveField'])) {
78
            $resolver = $info->parentType->config['resolveField'];
79
        } else {
80
            $resolver = $defaultResolver;
81
        }
82
83
        return $resolver;
84
    }
85
}
86