Completed
Push — master ( df9a93...1c763f )
by Rafael
04:17
created

IDToNodeTransformer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 17
dl 0
loc 99
ccs 18
cts 33
cp 0.5455
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B transform() 0 17 5
C reverseTransform() 0 40 11
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\Persistence\ObjectRepository;
14
use Symfony\Component\Form\DataTransformerInterface;
15
use Symfony\Component\Form\Exception\TransformationFailedException;
16
use Ynlo\GraphQLBundle\Model\NodeInterface;
17
use Ynlo\GraphQLBundle\Util\IDEncoder;
18
19
/**
20
 * Class IDToNodeTransformer
21
 */
22
class IDToNodeTransformer implements DataTransformerInterface
23
{
24
    /**
25
     * @var ObjectRepository|null
26
     */
27
    protected $repository;
28
29
    /**
30
     * @var array
31
     */
32
    protected $findBy = [];
33
34
    /**
35
     * IDToNodeTransformer constructor.
36
     *
37
     * @param ObjectRepository $repository
38
     * @param string|array     $findBy
39
     */
40 8
    public function __construct(ObjectRepository $repository = null, $findBy = null)
41
    {
42 8
        $this->repository = $repository;
43 8
        $this->findBy = (array) $findBy;
44 8
    }
45
46
    /**
47
     * Transforms an object (issue) to a string (number).
48
     *
49
     * @param NodeInterface|NodeInterface[] $node
50
     *
51
     * @return string|string[]
52
     */
53 8
    public function transform($node)
54
    {
55 8
        if (!$node) {
56 8
            return $node;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $node returns the type Ynlo\GraphQLBundle\Model...dle\Model\NodeInterface which is incompatible with the documented return type string|string[].
Loading history...
57
        }
58
59
        if (\is_array($node) || $node instanceof \Traversable) {
60
            $ids = [];
61
            /** @var array $node */
62
            foreach ($node as $n) {
63
                $ids[] = $this->transform($n);
64
            }
65
66
            return $ids;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ids returns an array which contains values of type string[] which are incompatible with the documented value type string.
Loading history...
67
        }
68
69
        return IDEncoder::encode($node);
70
    }
71
72
    /**
73
     * Transforms a string (id) to an object (node).
74
     *
75
     * @param string|string[]|mixed $globalId
76
     *
77
     * @return mixed
78
     *
79
     * @throws TransformationFailedException if object (issue) is not found.
80
     */
81 6
    public function reverseTransform($globalId)
82
    {
83 6
        if (!$globalId || \is_object($globalId)) {
84
            return $globalId;
85
        }
86
87 6
        if (\is_array($globalId)) {
88 2
            $nodes = [];
89
            /** @var array $globalId */
90 2
            foreach ($globalId as $id) {
91 2
                $nodes[] = $this->reverseTransform($id);
92
            }
93
94 2
            return $nodes;
95
        }
96
97 6
        $node = IDEncoder::decode($globalId);
98
99 6
        if (null === $node && $this->repository && $this->findBy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->findBy of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
100
            foreach ($this->findBy as $findBy) {
101
                $node = $this->repository->findOneBy([$findBy => $globalId]);
102
                if ($node) {
103
                    break;
104
                }
105
            }
106
        }
107
108 6
        if (null === $node) {
109
            // causes a validation error
110
            // this message is not shown to the user
111
            // see the invalid_message option
112
            throw new TransformationFailedException(
113
                sprintf(
114
                    'An node with id "%s" does not exist!',
115
                    $globalId
116
                )
117
            );
118
        }
119
120 6
        return $node;
121
    }
122
}
123