Completed
Push — master ( 533532...ee214c )
by Rafael
04:23
created

DataWithIdToNodeTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 48
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reverseTransform() 0 3 1
A __construct() 0 4 1
B transform() 0 13 5
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\ORM\EntityManagerInterface;
14
use Symfony\Component\Form\DataTransformerInterface;
15
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
16
use Ynlo\GraphQLBundle\Model\ID;
17
18
/**
19
 * Class IDToNodeTransformer
20
 */
21
class DataWithIdToNodeTransformer implements DataTransformerInterface
22
{
23
    /**
24
     * @var EntityManagerInterface
25
     */
26
    private $em;
27
28
    /**
29
     * @var Endpoint
30
     */
31
    protected $endpoint;
32
33
    /**
34
     * IDToNodeTransformer constructor.
35
     *
36
     * @param EntityManagerInterface $em
37
     * @param Endpoint               $endpoint
38
     */
39
    public function __construct(EntityManagerInterface $em, Endpoint $endpoint)
40
    {
41
        $this->em = $em;
42
        $this->endpoint = $endpoint;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function transform($data)
49
    {
50
        if (is_array($data) && isset($data['id'])) {
51
            $id = ID::createFromString($data['id']);
52
            if ($this->endpoint->hasType($id->getNodeType())) {
53
                $class = $this->endpoint->getClassForType($id->getNodeType());
54
                if ($class) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $class of type null|string is loosely compared to true; 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...
55
                    return $this->em->getRepository($class)->find($id->getDatabaseId());
56
                }
57
            }
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function reverseTransform($data)
67
    {
68
        return $data;
69
    }
70
}
71