Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

Node   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 25 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Query\Node;
12
13
use Ynlo\GraphQLBundle\Annotation as GraphQL;
14
use Ynlo\GraphQLBundle\Definition\ArgumentDefinition;
15
use Ynlo\GraphQLBundle\Model\ID;
16
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
17
18
/**
19
 * @GraphQL\Query(name="node")
20
 * @GraphQL\Argument(name="id", type="ID!")
21
 */
22
class Node extends AbstractResolver
23
{
24
    /**
25
     * @param mixed $id
26
     *
27
     * @return null|object
28
     */
29 3
    public function __invoke($id)
30
    {
31 3
        if ($id instanceof ID) {
32 1
            $type = $id->getNodeType();
33 1
            $searchValue = $id->getDatabaseId();
34 1
            $searchField = 'id';
35
        } else {
36
            //when use a different field to fetch,
37
            //@see QueryGet::fetchBy
38 2
            $searchValue = $id;
39
40 2
            $type = $this->getContext()->getDefinition()->getType();
41
42
            /** @var ArgumentDefinition $arg */
43 2
            $arg = array_values($this->getContext()->getDefinition()->getArguments())[0];
44
45 2
            $field = $this->getContext()->getDefinitionManager()->getType($type)->getField($arg->getName());
46 2
            $searchField = $field->getOriginName();
47
        }
48
49 3
        $entityClass = $this->getContext()->getDefinitionManager()->getClassForType($type);
50
51 3
        return $this->getManager()
52 3
                    ->getRepository($entityClass)
53 3
                    ->findOneBy([$searchField => $searchValue]);
54
    }
55
}
56