|
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\Error\NodeNotFoundException; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Model\NodeInterface; |
|
18
|
|
|
use Ynlo\GraphQLBundle\Util\TypeUtil; |
|
19
|
|
|
|
|
20
|
|
|
class SimpleIDEncoder implements IDEncoderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
private const DIVIDER = ':'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Registry |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $doctrine; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var DefinitionRegistry |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $definitionRegistry; |
|
33
|
|
|
|
|
34
|
22 |
|
|
|
35
|
|
|
public function __construct(DefinitionRegistry $definitionRegistry, Registry $registry) |
|
36
|
22 |
|
{ |
|
37
|
22 |
|
$this->doctrine = $registry; |
|
38
|
22 |
|
$this->definitionRegistry = $definitionRegistry; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritDoc} |
|
43
|
19 |
|
*/ |
|
44
|
|
|
public function encode(NodeInterface $node): ?string |
|
45
|
19 |
|
{ |
|
46
|
19 |
|
$nodeType = TypeUtil::resolveObjectType($this->definitionRegistry->getEndpoint(), $node); |
|
47
|
|
|
|
|
48
|
19 |
|
return sprintf('%s%s%s', $nodeType, self::DIVIDER, $node->getId()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
*/ |
|
54
|
12 |
|
public function decode($globalId): ?NodeInterface |
|
55
|
|
|
{ |
|
56
|
12 |
|
if (strpos($globalId, self::DIVIDER) > 1) { |
|
57
|
12 |
|
list($nodeType, $databaseId) = explode(self::DIVIDER, $globalId); |
|
58
|
|
|
|
|
59
|
12 |
|
$class = $this->definitionRegistry->getEndpoint()->getClassForType($nodeType); |
|
60
|
12 |
|
$manager = $this->doctrine->getManager(); |
|
61
|
12 |
|
if ($manager instanceof EntityManagerInterface) { |
|
|
|
|
|
|
62
|
12 |
|
$reference = $manager->getReference($class, $databaseId); |
|
63
|
|
|
$resolvedType = TypeUtil::resolveObjectType($this->definitionRegistry->getEndpoint(), $reference); |
|
64
|
|
|
|
|
65
|
|
|
//compare the given type encoded in the globalID with the type resolved by the object instance |
|
66
|
|
|
//This is important to avoid get a node using different type when use the same entity class |
|
67
|
|
|
//e.g. 'AdminUser:1' => resolve to type 'AdminUser' and should not be possible get using 'CommonUser:1' as globalId |
|
68
|
|
|
if ($resolvedType !== $nodeType) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $reference; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new \UnexpectedValueException('Not supported doctrine manager.'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|