reverseTransformSingleEntity()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\MediaBundle\DataTransformer;
4
5
use Alpixel\Bundle\MediaBundle\Entity\Media;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\QueryBuilder;
9
use Symfony\Component\Form\DataTransformerInterface;
10
use Symfony\Component\Form\Exception\TransformationFailedException;
11
use Symfony\Component\Form\Exception\UnexpectedTypeException;
12
13
/**
14
 * Data transformation class.
15
 *
16
 * @author Gregwar <[email protected]>
17
 */
18
class EntityToIdTransformer implements DataTransformerInterface
19
{
20
    protected $em;
21
    private $class;
22
    private $multiple;
23
24
    public function __construct(EntityManager $em, $class, $property, $queryBuilder, $multiple)
0 ignored issues
show
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        if (!(null === $queryBuilder || $queryBuilder instanceof QueryBuilder || $queryBuilder instanceof \Closure)) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\QueryBuilder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
27
            throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder or \Closure');
28
        }
29
30
        if (null === $class) {
31
            throw new UnexpectedTypeException($class, 'string');
32
        }
33
34
        $this->em = $em;
35
        $this->class = $class;
36
        $this->multiple = $multiple;
37
    }
38
39
    public function transform($data)
40
    {
41
        if ($data instanceof Collection) {
42
            return $this->reverseTransform($data);
43
        }
44
45
        if (null === $data) {
46
            return;
47
        }
48
49
        if (!$this->multiple) {
50
            return $this->transformSingleEntity($data);
51
        }
52
53
        if (is_array($data))
54
            return $data;
55
56
        $return = [];
57
        $data = explode('#&#', $data);
58
59
        foreach ($data as $element) {
60
            $return[] = $this->transformSingleEntity($element);
61
        }
62
63
        return $return;
64
    }
65
66
    protected function transformSingleEntity($data)
67
    {
68
        if ($data instanceof Media) {
69
            return $data->getSecretKey();
70
        } else {
71
            return $this->em->getRepository($this->class)->findOneBySecretKey($data);
72
        }
73
    }
74
75
    public function reverseTransform($data)
76
    {
77
        if (!$data) {
78
            return;
79
        }
80
81
        if (!($data instanceof Collection)) {
82
            return $this->transform($data);
83
        }
84
85
        $return = [];
86
87
        foreach ($data as $element) {
88
            $return[] = $this->reverseTransformSingleEntity($element);
89
        }
90
91
        return implode('#&#', $return);
92
    }
93
94
    protected function reverseTransformSingleEntity($data)
95
    {
96
        if (!($data instanceof Media)) {
97
            throw new TransformationFailedException('Can not find entity');
98
        }
99
100
        return $data->getSecretKey();
101
    }
102
}
103