Completed
Push — master ( df309f...9e12f0 )
by Rafael
08:32
created

SimpleIDEncoder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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\Encoder;
12
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
16
use Ynlo\GraphQLBundle\Model\NodeInterface;
17
use Ynlo\GraphQLBundle\Util\TypeUtil;
18
19
class SimpleIDEncoder implements IDEncoderInterface
20
{
21
    private const DIVIDER = ':';
22
23
    /**
24
     * @var Registry
25
     */
26
    protected $doctrine;
27
28
    /**
29
     * @var DefinitionRegistry
30
     */
31
    protected $definitionRegistry;
32
33
34 7
    public function __construct(DefinitionRegistry $definitionRegistry, Registry $registry)
35
    {
36 7
        $this->doctrine = $registry;
37 7
        $this->definitionRegistry = $definitionRegistry;
38 7
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 4
    public function encode(NodeInterface $node): ?string
44
    {
45 4
        $nodeType = TypeUtil::resolveObjectType($this->definitionRegistry->getEndpoint(), $node);
46
47 4
        return sprintf('%s%s%s', $nodeType, self::DIVIDER, $node->getId());
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 6
    public function decode($globalId): ?NodeInterface
54
    {
55 6
        if (strpos($globalId, self::DIVIDER) > 1) {
56 5
            list($nodeType, $databaseId) = explode(self::DIVIDER, $globalId);
57
58 5
            $class = $this->definitionRegistry->getEndpoint()->getClassForType($nodeType);
59 5
            if (!class_exists($class)) {
60 1
                return null;
61
            }
62
63 4
            $manager = $this->doctrine->getManager();
64 4
            if ($manager instanceof EntityManagerInterface) {
0 ignored issues
show
introduced by
$manager is always a sub-type of Doctrine\ORM\EntityManagerInterface.
Loading history...
65 4
                $reference = $manager->getReference($class, $databaseId);
66 4
                $resolvedType = TypeUtil::resolveObjectType($this->definitionRegistry->getEndpoint(), $reference);
67
68
                //compare the given type encoded in the globalID with the type resolved by the object instance
69
                //This is important to avoid get a node using different type when use the same entity class
70
                //e.g. 'AdminUser:1' => resolve to type 'AdminUser' and should not be possible get using 'CommonUser:1' as globalId
71 4
                if ($resolvedType !== $nodeType) {
72
                    return null;
73
                }
74
75 4
                return $reference;
76
            }
77
78
            throw new \UnexpectedValueException('Not supported doctrine manager.');
79
        }
80
81 1
        return null;
82
    }
83
}
84