Completed
Push — master ( df309f...9e12f0 )
by Rafael
08:32
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 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 5
    public function __construct(ObjectRepository $repository = null, $findBy = null)
41
    {
42 5
        $this->repository = $repository;
43 5
        $this->findBy = (array) $findBy;
44 5
    }
45
46
    /**
47
     * Transforms an object (issue) to a string (number).
48
     *
49
     * @param NodeInterface|NodeInterface[] $node
50
     *
51
     * @return string|string[]
52
     */
53 2
    public function transform($node)
54
    {
55 2
        if (!$node) {
56 2
            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 1
        if (\is_array($node) || $node instanceof \Traversable) {
60 1
            $ids = [];
61
            /** @var array $node */
62 1
            foreach ($node as $n) {
63 1
                $ids[] = $this->transform($n);
64
            }
65
66 1
            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 1
        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 3
    public function reverseTransform($globalId)
82
    {
83 3
        if (!$globalId || \is_object($globalId)) {
84 1
            return $globalId;
85
        }
86
87 3
        if (\is_array($globalId)) {
88 1
            $nodes = [];
89
            /** @var array $globalId */
90 1
            foreach ($globalId as $id) {
91 1
                $nodes[] = $this->reverseTransform($id);
92
            }
93
94 1
            return $nodes;
95
        }
96
97 3
        $node = IDEncoder::decode($globalId);
98
99 3
        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 1
            foreach ($this->findBy as $findBy) {
101 1
                $node = $this->repository->findOneBy([$findBy => $globalId]);
102 1
                if ($node) {
103 1
                    break;
104
                }
105
            }
106
        }
107
108 3
        if (null === $node) {
109
            // causes a validation error
110
            // this message is not shown to the user
111
            // see the invalid_message option
112 1
            throw new TransformationFailedException(
113 1
                sprintf(
114 1
                    'An node with id "%s" does not exist!',
115 1
                    $globalId
116
                )
117
            );
118
        }
119
120 2
        return $node;
121
    }
122
}
123