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

IDToNodeTransformer::transform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 9.6666
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\Form\DataTransformer;
12
13
use Doctrine\Common\Util\ClassUtils;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Symfony\Component\Form\DataTransformerInterface;
16
use Symfony\Component\Form\Exception\TransformationFailedException;
17
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager;
18
use Ynlo\GraphQLBundle\Model\ID;
19
use Ynlo\GraphQLBundle\Model\NodeInterface;
20
21
/**
22
 * Class IDToNodeTransformer
23
 */
24
class IDToNodeTransformer implements DataTransformerInterface
25
{
26
    /**
27
     * @var EntityManagerInterface
28
     */
29
    private $em;
30
31
    /**
32
     * @var DefinitionManager
33
     */
34
    protected $dm;
35
36
    /**
37
     * IDToNodeTransformer constructor.
38
     *
39
     * @param EntityManagerInterface $em
40
     * @param DefinitionManager      $definitionManager
41
     */
42 4
    public function __construct(EntityManagerInterface $em, DefinitionManager $definitionManager)
43
    {
44 4
        $this->em = $em;
45 4
        $this->dm = $definitionManager;
46 4
    }
47
48
    /**
49
     * Transforms an object (issue) to a string (number).
50
     *
51
     * @param  NodeInterface|null $node
52
     *
53
     * @return string
54
     */
55 4
    public function transform($node)
56
    {
57 4
        if (null === $node) {
58 4
            return '';
59
        }
60
61
        $nodeType = $this->dm->getTypeForClass(ClassUtils::getRealClass($node));
0 ignored issues
show
Bug introduced by
$node of type Ynlo\GraphQLBundle\Model\NodeInterface is incompatible with the type string expected by parameter $class of Doctrine\Common\Util\ClassUtils::getRealClass(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $nodeType = $this->dm->getTypeForClass(ClassUtils::getRealClass(/** @scrutinizer ignore-type */ $node));
Loading history...
62
63
        return ID::encode($nodeType, $node->getId());
64
    }
65
66
    /**
67
     * Transforms a string (id) to an object (node).
68
     *
69
     * @param  string $globalId
70
     *
71
     * @return NodeInterface|null
72
     *
73
     * @throws TransformationFailedException if object (issue) is not found.
74
     */
75 4
    public function reverseTransform($globalId)
76
    {
77 4
        if (!$globalId) {
78
            return null;
79
        }
80
81 4
        $id = ID::createFromString($globalId);
82
83 4
        if (!$id || !$id->getNodeType() || !$this->dm->hasType($id->getNodeType())) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id->getNodeType() of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84
            return null;
85
        }
86
87 4
        $node = $this->em
88 4
            ->getRepository($this->dm->getClassForType($id->getNodeType()))
89 4
            ->find($id->getDatabaseId());
90
91 4
        if (null === $node) {
92
            // causes a validation error
93
            // this message is not shown to the user
94
            // see the invalid_message option
95
            throw new TransformationFailedException(
96
                sprintf(
97
                    'An node with id "%s" does not exist!',
98
                    $id
99
                )
100
            );
101
        }
102
103 4
        return $node;
104
    }
105
}
106