Passed
Push — master ( 5cd32c...1a1aa9 )
by Rafael
05:46
created

Node::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5.3197

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 4
cts 11
cp 0.3636
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 5.3197
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\Error\NodeNotFoundException;
15
use Ynlo\GraphQLBundle\Model\NodeInterface;
16
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
17
18
/**
19
 * @GraphQL\Query(name="node")
20
 * @GraphQL\Argument(name="id", type="ID!", internalName="node")
21
 */
22
class Node extends AbstractResolver
23
{
24
    protected $fetchBy = 'id';
25
26
    /**
27
     * @param mixed $node
28
     *
29
     * @return null|object
30
     */
31 3
    public function __invoke($node)
32
    {
33 3
        if (!$node) {
34
            throw new NodeNotFoundException();
35
        }
36
37 3
        if ($node instanceof NodeInterface) {
38 3
            return $node;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $node returns the type Ynlo\GraphQLBundle\Model\NodeInterface which is incompatible with the documented return type object|null.
Loading history...
39
        }
40
41
        //when use a different field to fetch,
42
        //@see QueryGet::fetchBy
43
        $searchValue = $node;
44
45
        $type = $this->getContext()->getNodeDefinition()->getName();
46
47
        $entityClass = $this->getContext()->getEndpoint()->getClassForType($type);
48
49
        return $this->getManager()
50
                    ->getRepository($entityClass)
51
                    ->findOneBy([$this->fetchBy => $searchValue]);
52
    }
53
}
54