CallableGuesser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 44
c 0
b 0
f 0
rs 10

3 Methods

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