DoctrineResolver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 60
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
B createFunction() 0 33 5
1
<?php
2
3
namespace Arthem\GraphQLMapper\Schema\Resolve;
4
5
use Arthem\GraphQLMapper\Mapping\Field;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use GraphQL\Type\Definition\ListOfType;
8
use GraphQL\Type\Definition\ResolveInfo;
9
10
class DoctrineResolver extends SingletonResolver
11
{
12
    /**
13
     * @var ObjectManager
14
     */
15
    private $om;
16
17
    /**
18
     * @param ObjectManager $om
19
     */
20
    public function __construct(ObjectManager $om)
21
    {
22
        $this->om = $om;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getName()
29
    {
30
        return 'doctrine';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function createFunction(array $config, Field $field)
37
    {
38
        return function ($node, array $arguments, ResolveInfo $info) {
39
            $resolveConfig = $this->getResolveConfig($info);
40
41
            $resolveConfig = $resolveConfig + [
42
                    'array_params' => false,
43
                ];
44
45
            if (!isset($resolveConfig['method'])) {
46
                $resolveConfig['method']       = $info->returnType instanceof ListOfType ?
47
                    'findBy' :
48
                    'findOneBy';
49
                $resolveConfig['array_params'] = true;
50
            }
51
52
            $repository = $this->om->getRepository($resolveConfig['entity']);
53
54
            $params = $arguments;
55
56
            if ($resolveConfig['array_params']) {
57
                $params = [$params];
58
            }
59
60
            if (null !== $node) {
61
                array_unshift($params, $node);
62
            }
63
64
            $result = call_user_func_array([$repository, $resolveConfig['method']], $params);
65
66
            return $result;
67
        };
68
    }
69
}
70