IdToMediaTransformer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 73
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A transform() 0 18 4
A reverseTransform() 0 15 4
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Form\Type;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Kunstmaan\MediaBundle\Entity\Media;
8
use Symfony\Component\Form\DataTransformerInterface;
9
use Symfony\Component\Form\Exception\TransformationFailedException;
10
use Symfony\Component\Form\Exception\UnexpectedTypeException;
11
12
/**
13
 * IdToMediaTransformer
14
 */
15
class IdToMediaTransformer implements DataTransformerInterface
16
{
17
    /**
18
     * @var ObjectManager
19
     */
20
    private $objectManager;
21
22
    /**
23
     * @var CurrentValueContainer
24
     */
25
    private $currentValueContainer;
26
27
    /**
28
     * @param ObjectManager         $objectManager         The object manager
29
     * @param CurrentValueContainer $currentValueContainer The current value container
30
     */
31
    public function __construct(ObjectManager $objectManager, CurrentValueContainer $currentValueContainer)
32
    {
33
        $this->objectManager = $objectManager;
34
        $this->currentValueContainer = $currentValueContainer;
35
    }
36
37
    /**
38
     * @param Media $entity The value in the original representation
39
     *
40
     * @return mixed The value in the transformed representation
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|array<string,Media|integer>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
41
     *
42
     * @throws UnexpectedTypeException   when the argument is not an object
43
     * @throws \InvalidArgumentException when the parameter is a collection
44
     */
45
    public function transform($entity)
46
    {
47
        if (empty($entity)) {
48
            return '';
49
        }
50
        if (!\is_object($entity)) {
51
            throw new UnexpectedTypeException($entity, 'object');
52
        }
53
        if ($entity instanceof Collection) {
54
            throw new \InvalidArgumentException('Expected an object, but got a collection. Did you forget to pass "multiple=true" to an entity field?');
55
        }
56
        $this->currentValueContainer->setCurrentValue($entity);
57
58
        return array(
59
            'ent' => $entity,
60
            'id' => $entity->getId(),
61
        );
62
    }
63
64
    /**
65
     * @param string $key
66
     *
67
     * @return Media
0 ignored issues
show
Documentation introduced by
Should the return type not be Media|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     *
69
     * @throws UnexpectedTypeException       when the parameter is not numeric
70
     * @throws TransformationFailedException when the media item cannot be loaded/found
71
     */
72
    public function reverseTransform($key)
73
    {
74
        if (empty($key)) {
75
            return null;
76
        }
77
        if (!is_numeric($key)) {
78
            throw new UnexpectedTypeException($key, 'numeric');
79
        }
80
        if (!($entity = $this->objectManager->getRepository(Media::class)->find($key))) {
81
            throw new TransformationFailedException(sprintf('The entity with key "%s" could not be found', $key));
82
        }
83
        $this->currentValueContainer->setCurrentValue($entity);
84
85
        return $entity;
86
    }
87
}
88