UploadableDataTransformer::transform()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace Fenrizbes\UploadableBundle\Form\DataTransformer;
4
5
use Fenrizbes\UploadableBundle\File\UploadableFile;
6
use Symfony\Component\Form\DataTransformerInterface;
7
use Symfony\Component\HttpFoundation\File\File;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class UploadableDataTransformer implements DataTransformerInterface
11
{
12
    protected $root_dir;
13
14
    public function __construct($root_dir)
15
    {
16
        $this->root_dir = $root_dir;
17
    }
18
19
    public function transform($data)
20
    {
21
        if (!is_array($data)) {
22
            $data = array(
23
                'file' => $data
24
            );
25
        }
26
27
        if (!is_null($data['file']) && !$data['file'] instanceof File) {
28
            $data['file'] = new UploadableFile($this->root_dir, $data['file']);
29
        }
30
31
        return $data;
32
    }
33
34
    public function reverseTransform($data)
35
    {
36
        if (!is_array($data) || !isset($data['file']) || !$data['file'] instanceof File) {
37
            return null;
38
        }
39
        
40
        if ($data['file'] instanceof UploadableFile) {
41
            return $data['file']->getWebPath();
42
        }
43
        
44
        return $data['file'];
45
    }
46
}
47