Completed
Push — master ( 895aee...c5d013 )
by Arthur
8s
created

CallableGuesser::guessFieldResolveConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Arthem\GraphQLMapper\Mapping\Guesser;
3
4
use Arthem\GraphQLMapper\Mapping\Context\ContainerContext;
5
use Arthem\GraphQLMapper\Mapping\Context\FieldContext;
6
7
class CallableGuesser implements TypeResolveGuesserInterface, FieldResolveGuesserInterface
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    public function guessFieldResolveConfig(FieldContext $fieldContext)
13
    {
14
        return $this->guessResolveConfig($fieldContext->getField()->getResolveConfig());
15
    }
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function guessTypeResolveConfig(ContainerContext $containerContext)
21
    {
22
        return $this->guessResolveConfig($containerContext->getContainer()->getResolveConfig());
23
    }
24
25
    /**
26
     * @param array $resolveConfig
27
     * @return ResolveConfigGuess
28
     */
29
    private function guessResolveConfig(array $resolveConfig)
30
    {
31
        if (empty($resolveConfig['function'])) {
32
            return;
33
        }
34
35
        $function = $resolveConfig['function'];
36
37
        if (preg_match('#^([\w_]+)\:\:([\w_]+)$#', $resolveConfig['function'], $regs)) {
38
            $function = [$regs[1], $regs[2]];
39
        }
40
41
        if (!is_callable($function)) {
42
            throw new \InvalidArgumentException(sprintf('Method or function "%s" is not callable', $resolveConfig['function']));
43
        }
44
45
        return new ResolveConfigGuess([
46
            'handler'  => 'callable',
47
            'function' => $function,
48
        ]);
49
    }
50
}
51