Passed
Push — master ( 898411...5b804b )
by Andrey
56s queued 13s
created

ObjectField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 10
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Field;
6
7
use Andi\GraphQL\Common\ResolverArguments;
8
use GraphQL\Type\Definition as Webonyx;
9
use Spiral\Core\InvokerInterface;
10
use Spiral\Core\ScopeInterface;
11
12
final class ObjectField extends Webonyx\FieldDefinition
13
{
14
    /**
15
     * @param array $config
16
     * @param string $method
17
     * @param array<string,string> $argumentsMap
18
     * @param ScopeInterface $scope
19
     * @param InvokerInterface $invoker
20
     */
21 3
    public function __construct(
22
        array $config,
23
        private readonly string $method,
24
        private readonly array $argumentsMap,
25
        private readonly ScopeInterface $scope,
26
        private readonly InvokerInterface $invoker,
27
    ) {
28 3
        $config['resolve'] = $this->resolve(...);
29
30 3
        parent::__construct($config);
31
    }
32
33
    /**
34
     * @param mixed $object
35
     * @param array<string,mixed> $args
36
     * @param mixed $context
37
     * @param Webonyx\ResolveInfo $info
38
     *
39
     * @return mixed
40
     */
41 3
    private function resolve(
42
        mixed $object,
43
        array $args,
44
        mixed $context,
45
        Webonyx\ResolveInfo $info,
46
    ): mixed {
47 3
        $parameters = [];
48 3
        foreach ($args as $name => $value) {
49 3
            $parameters[$this->argumentsMap[$name] ?? $name] = $value;
50
        }
51
52 3
        return $this->scope->runScope(
53 3
            [ResolverArguments::class => new ResolverArguments($object, $args, $context, $info)],
54 3
            fn () => $this->invoker->invoke([$object, $this->method], $parameters),
55 3
        );
56
    }
57
}
58