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

CallableGuesser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 44
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
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