Passed
Push — master ( 5cd32c...1a1aa9 )
by Rafael
05:46
created

IDToNodeTransformer::__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
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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 Symfony\Component\Form\DataTransformerInterface;
14
use Symfony\Component\Form\Exception\TransformationFailedException;
15
use Ynlo\GraphQLBundle\Model\NodeInterface;
16
use Ynlo\GraphQLBundle\Util\IDEncoder;
17
18
/**
19
 * Class IDToNodeTransformer
20
 */
21
class IDToNodeTransformer implements DataTransformerInterface
22
{
23
    /**
24
     * Transforms an object (issue) to a string (number).
25
     *
26
     * @param NodeInterface|NodeInterface[] $node
27
     *
28
     * @return string|string[]
29
     */
30 8
    public function transform($node)
31
    {
32 8
        if (!$node) {
33 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...
34
        }
35
36
        if (\is_array($node) || $node instanceof \Traversable) {
37
            $ids = [];
38
            /** @var array $node */
39
            foreach ($node as $n) {
40
                $ids[] = $this->transform($n);
41
            }
42
43
            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...
44
        }
45
46
        return IDEncoder::encode($node);
47
    }
48
49
    /**
50
     * Transforms a string (id) to an object (node).
51
     *
52
     * @param string|string[]|mixed $globalId
53
     *
54
     * @return mixed
55
     *
56
     * @throws TransformationFailedException if object (issue) is not found.
57
     */
58 6
    public function reverseTransform($globalId)
59
    {
60 6
        if (!$globalId || \is_object($globalId)) {
61
            return $globalId;
62
        }
63
64 6
        if (\is_array($globalId)) {
65 2
            $nodes = [];
66
            /** @var array $globalId */
67 2
            foreach ($globalId as $id) {
68 2
                $nodes[] = $this->reverseTransform($id);
69
            }
70
71 2
            return $nodes;
72
        }
73
74 6
        $node = IDEncoder::decode($globalId);
75
76 6
        if (null === $node) {
77
            // causes a validation error
78
            // this message is not shown to the user
79
            // see the invalid_message option
80
            throw new TransformationFailedException(
81
                sprintf(
82
                    'An node with id "%s" does not exist!',
83
                    $globalId
84
                )
85
            );
86
        }
87
88 6
        return $node;
89
    }
90
}
91