MediaDataTransformer::reverseTransform()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
namespace MediaBundle\Form\DataTransformer;
4
5
use MediaBundle\Model\Media;
6
use Doctrine\ORM\EntityManager;
7
use MediaBundle\Manager\MediaManagerInterface;
8
use Symfony\Component\Form\DataTransformerInterface;
9
use Symfony\Component\Form\Exception\TransformationFailedException;
10
11
class MediaDataTransformer implements DataTransformerInterface
12
{
13
14
    /**
15
     *
16
     * @var MediaManagerInterface
17
     */
18
    private $manager;
19
20
    /**
21
     *
22
     * @var EntityManager
23
     */
24
    private $entityManager;
25
    
26
    /**
27
     *
28
     * @var array
29
     */
30
    private $options = array();
31
    
32
    /**
33
     * 
34
     * @param MediaManagerInterface $manager
35
     * @param EntityManager $entityManager
36
     * @param array $options
37
     */
38
    public function __construct(MediaManagerInterface $manager, EntityManager $entityManager, $options = [])
39
    {
40
        $this->manager = $manager;
41
        $this->entityManager = $entityManager;
42
        $this->options = $options;
43
    }
44
45
    /**
46
     * 
47
     * @param Media|array|null $value
48
     * 
49
     * @return Media|null
50
     * 
51
     * @throws TransformationFailedException
52
     */
53
    public function reverseTransform($value)
54
    {
55
56
        if ($value instanceof Media) {
57
            return $this->manager->save($value, $this->options);
58
        } else if (is_null($value)) {
59
            return $value;
60
        }
61
        throw new TransformationFailedException();
62
    }
63
64
    /**
65
     * 
66
     * @param string|null $value
67
     * 
68
     * @return string|null
69
     */
70
    public function transform($value)
71
    {
72
        return $value;
73
    }
74
75
}
76