Completed
Push — master ( 9e9a0d...740d0f )
by Rafael
04:13
created

Node::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 18
ccs 8
cts 10
cp 0.8
crap 2.032
rs 9.4285
c 0
b 0
f 0
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\Model\ID;
15
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
16
17
/**
18
 * @GraphQL\Query(name="node")
19
 * @GraphQL\Argument(name="id", type="ID!")
20
 */
21
class Node extends AbstractResolver
22
{
23
    protected $fetchBy = 'id';
24
25
    /**
26
     * @param mixed $id
27
     *
28
     * @return null|object
29
     */
30 2
    public function __invoke($id)
31
    {
32 2
        if ($id instanceof ID) {
33 2
            $type = $id->getNodeType();
34 2
            $searchValue = $id->getDatabaseId();
35
        } else {
36
            //when use a different field to fetch,
37
            //@see QueryGet::fetchBy
38
            $searchValue = $id;
39
40
            $type = $this->getContext()->getNodeDefinition()->getName();
41
        }
42
43 2
        $entityClass = $this->getContext()->getEndpoint()->getClassForType($type);
44
45 2
        return $this->getManager()
46 2
                    ->getRepository($entityClass)
47 2
                    ->findOneBy([$this->fetchBy => $searchValue]);
48
    }
49
}
50