Completed
Push — master ( 23bc34...6717d0 )
by Zlatin
02:26
created

src/Form/DataTransformer/MediaTransformer.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Media\Form\DataTransformer;
4
5
use Media\Entity\Media;
6
use Doctrine\ORM\EntityManager;
7
use Symfony\Component\Form\DataTransformerInterface;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
use Symfony\Component\Form\Exception\TransformationFailedException;
10
11
class MediaTransformer implements DataTransformerInterface
12
{
13
14
    /**
15
     *
16
     * @var \Silex\Application 
17
     */
18
    private $app;
19
    
20
    /**
21
     *
22
     * @var \Media\Provider\ImageProvider
23
     */
24
    private $provider;
25
    
26
    /**
27
     *
28
     * @var EntityManager
29
     */
30
    private $entityManager;
31
32
    /**
33
     * 
34
     * @param \Silex\Application $app
35
     * @param array $options
36
     */
37
    public function __construct(\Silex\Application $app, $options = array())
38
    {
39
        $this->app = $app;
40
        $this->options = $options;
0 ignored issues
show
The property options does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
        $this->provider = new \Media\Provider\ImageProvider($app);
42
        $this->entityManager = $this->app['orm.em'];
43
    }
44
45
    /**
46
     * 
47
     * @param Media|null $value
48
     * 
49
     * @return integer
50
     */
51
    public function transform($value)
52
    {
53
        return $value;
54
    }
55
56
    /**
57
     * 
58
     * @param integer|Media|UploadedFile $value
59
     * 
60
     * @return Media
61
     * 
62
     * @throws TransformationFailedException
63
     */
64
    public function reverseTransform($value = null)
65
    {
66
        if (null === $value) {
67
            return null;
68
        }
69
        else if ($value->getBinary()) {
70
            return $this->provider->save($value->getBinary(), $value);
71
        }
72
        else if ($value->getId()) {
73
            return $value;
74
        }
75
        
76
        $media = $this->entityManager->getRepository('Media\Entity\Media')->find($value->getId());
77
        
78
        if ($media instanceof Media) {
79
            return $media;
80
        }
81
        
82
        throw new TransformationFailedException();
83
    }
84
85
}
86