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 (#7)
by Jérémiah
09:31
created

FieldsConfigSolution::solve()   C

Complexity

Conditions 13
Paths 29

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 15.872

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 49
ccs 26
cts 35
cp 0.7429
rs 5.1401
cc 13
eloc 27
nc 29
nop 2
crap 15.872

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Config;
13
14
use Overblog\GraphQLBundle\Error\UserError;
15
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
16
use Overblog\GraphQLBundle\Relay\Connection\Output\Edge;
17
use Overblog\GraphQLBundle\Resolver\ResolverInterface;
18
19
class FieldsConfigSolution extends AbstractConfigSolution implements UniqueConfigSolutionInterface
20
{
21
    /**
22
     * @var TypeConfigSolution
23
     */
24
    private $typeConfigSolution;
25
26
    /**
27
     * @var ResolveCallbackConfigSolution
28
     */
29
    private $resolveCallbackConfigSolution;
30
31 31
    public function __construct(
32
        TypeConfigSolution $typeConfigSolution,
33
        ResolveCallbackConfigSolution $resolveCallbackConfigSolution
34
    ) {
35 31
        $this->typeConfigSolution = $typeConfigSolution;
36 31
        $this->resolveCallbackConfigSolution = $resolveCallbackConfigSolution;
37 31
    }
38
39 26
    public function solve($values, $config = null)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41 26
        foreach ($values as $field => &$options) {
42 26
            if (isset($options['builder'])) {
43 19
                $builderConfig = isset($options['builderConfig']) ? $options['builderConfig'] : [];
44
45 19
                $access = isset($options['access']) ? $options['access'] : null;
46 19
                $options = $this->solveBuilder($options['builder'], $builderConfig, $this->fieldResolver, $field);
47 19
                $options['access'] = $access;
48 19
                $options = $this->resolveResolveAndAccessIfNeeded($options);
49
50 19
                unset($options['builderConfig'], $options['builder']);
51
52 19
                continue;
53
            }
54
55 26
            if (isset($options['type'])) {
56 26
                $options['type'] = $this->typeConfigSolution->solveTypeCallback($options['type']);
57 26
            }
58
59 26
            if (isset($options['args'])) {
60
                foreach ($options['args'] as &$argsOptions) {
61
                    $argsOptions['type'] = $this->typeConfigSolution->solveTypeCallback($argsOptions['type']);
62
                    if (isset($argsOptions['defaultValue'])) {
63
                        $argsOptions['defaultValue'] = $this->solveUsingExpressionLanguageIfNeeded($argsOptions['defaultValue']);
64
                    }
65
                }
66
            }
67
68 26
            if (isset($options['argsBuilder'])) {
69 7
                $argsBuilderConfig = isset($options['argsBuilder']['config']) ? $options['argsBuilder']['config'] : [];
70
71 7
                $options['args'] = array_merge(
72 7
                    $this->solveBuilder($options['argsBuilder']['builder'], $argsBuilderConfig, $this->argResolver),
73 7
                    isset($options['args']) ? $options['args'] : []
74 7
                );
75
76 7
                unset($options['argsBuilder']);
77 7
            }
78
79 26
            $options = $this->resolveResolveAndAccessIfNeeded($options);
80
81 26
            if (isset($options['deprecationReason'])) {
82
                $options['deprecationReason'] = $this->solveUsingExpressionLanguageIfNeeded($options['deprecationReason']);
83
            }
84 26
        }
85
86 26
        return $values;
87
    }
88
89 26
    private function solveBuilder($rawBuilder, array $rawBuilderConfig, ResolverInterface $builderResolver, $name = null)
90
    {
91 26
        $builder = is_callable($rawBuilder) ? $rawBuilder : $builderResolver->resolve($rawBuilder);
92 26
        $builderConfig = [];
93 26
        if (!empty($rawBuilderConfig)) {
94 19
            $builderConfig = $rawBuilderConfig;
95
96 19
            if (!is_array($builderConfig)) {
97
                $builderConfig = [$builderConfig];
98
            }
99 19
            $builderConfig = $this->configResolver->resolve($builderConfig);
100 19
        }
101
102 26
        if (null !== $name) {
103 19
            $builderConfig['name'] = $name;
104 19
        }
105
106 26
        return call_user_func_array([$builder, 'toMappingDefinition'], [$builderConfig]);
107
    }
108
109 26
    private function resolveResolveAndAccessIfNeeded(array $options)
110
    {
111 26
        $treatedOptions = $options;
112
113 26
        if (isset($treatedOptions['resolve'])) {
114 26
            $treatedOptions['resolve'] = $this->resolveCallbackConfigSolution->solve($treatedOptions['resolve']);
115 26
        }
116
117 26
        if (isset($treatedOptions['access'])) {
118
            $resolveCallback = $this->configResolver->getDefaultResolveFn();
119
120
            if (isset($treatedOptions['resolve'])) {
121
                $resolveCallback = $treatedOptions['resolve'];
122
            }
123
124
            $treatedOptions['resolve'] = $this->resolveAccessAndWrapResolveCallback($treatedOptions['access'], $resolveCallback);
125
        }
126 26
        unset($treatedOptions['access']);
127
128 26
        return $treatedOptions;
129
    }
130
131 5
    private function resolveAccessAndWrapResolveCallback($expression, callable $resolveCallback = null)
132
    {
133
        return function () use ($expression, $resolveCallback) {
134 5
            $args = func_get_args();
135
136 5
            $result = null !== $resolveCallback  ? call_user_func_array($resolveCallback, $args) : null;
137
138 5
            $values = call_user_func_array([$this, 'solveResolveCallbackArgs'], $args);
139
140
            $checkAccess = function ($object, $throwException = false) use ($expression, $values) {
141
                try {
142 5
                    $access = $this->solveUsingExpressionLanguageIfNeeded(
143 5
                        $expression,
144 5
                        array_merge($values, ['object' => $object])
145 5
                    );
146 5
                } catch (\Exception $e) {
147 1
                    $access = false;
148
                }
149
150 5
                if ($throwException && !$access) {
151 2
                    throw new UserError('Access denied to this field.');
152
                }
153
154 3
                return $access;
155 5
            };
156
157 5
            switch (true) {
158 5
                case is_array($result) || $result instanceof \ArrayAccess:
159 1
                    $result = array_filter(
160 1
                        array_map(
161
                            function ($object) use ($checkAccess) {
162 1
                                return $checkAccess($object) ? $object : null;
163 1
                            },
164
                            $result
165 1
                        )
166 1
                    );
167 1
                    break;
168
169 4
                case $result instanceof Connection:
170 1
                    $result->edges = array_map(
171 1
                        function (Edge $edge) use ($checkAccess) {
172 1
                            $edge->node = $checkAccess($edge->node) ? $edge->node : null;
173
174 1
                            return $edge;
175 1
                        },
176 1
                        $result->edges
177 1
                    );
178 1
                    break;
179
180 3
                default:
181 3
                    $checkAccess($result, true);
182 1
                    break;
183 3
            }
184
185 3
            return $result;
186 5
        };
187
    }
188
}
189