MediaManager::save()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 28
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 8
nop 2
crap 42
1
<?php
2
3
namespace MediaBundle\Manager;
4
5
use MediaBundle\Model\Media;
6
use Doctrine\ORM\EntityManager;
7
use MediaBundle\Manager\MediaManagerInterface;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class MediaManager implements MediaManagerInterface
11
{
12
13
    /**
14
     *
15
     * @var EntityManager
16
     */
17
    private $entityManager;
18
19
    /**
20
     *
21
     * @var string
22
     */
23
    private $kernelRootDir;
24
    
25
    /**
26
     *
27
     * @var string
28
     */
29
    private $uploadPath;
30
    
31
    /**
32
     * 
33
     * @param EntityManager $entityManager
34
     * @param string $kernelRootDir
35
     * @param string $uploadPath
36
     */
37
    public function __construct(EntityManager $entityManager, $kernelRootDir, $uploadPath)
38
    {
39
        $this->entityManager = $entityManager;
40
        $this->kernelRootDir = $kernelRootDir;
41
        $this->uploadPath = $uploadPath;
42
    }
43
44
    /**
45
     * 
46
     * @param UploadedFile $file
47
     * @param string $directory
48
     * 
49
     * @return string
50
     */
51
    public function generateName(UploadedFile $file, $directory)
52
    {
53
        $extension = $file->guessExtension();
54
        do {
55
            $filename = md5(uniqid()) . '.' . $extension;
56
            if (file_exists($directory . DIRECTORY_SEPARATOR . $filename)) {
57
                $filename = null;
58
            }
59
        } while (is_null($filename));
60
        return $filename;
61
    }
62
63
    /**
64
     * 
65
     * @param UploadedFile $file
66
     * 
67
     * @return array
68
     */
69
    public function updateMetaData(UploadedFile $file)
70
    {
71
        return [
72
            'filename' => $file->getClientOriginalName(),
73
            'filesize' => $file->getClientSize(),
74
            'filetype' => $file->getClientMimeType(),
75
            'extension' => $file->getClientOriginalExtension()
76
        ];
77
    }
78
79
    /**
80
     * 
81
     * @param Media $media
82
     * @param array $options
83
     * 
84
     * @return Media
85
     */
86
    public function save(Media $media, array $options = [])
87
    {
88
89
        if (!$media->getFile() instanceof UploadedFile) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpFoundation\File\UploadedFile 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...
90
            return $media->getId() ? $media : null;
91
        }
92
93
        $directory = implode(DIRECTORY_SEPARATOR, array($this->kernelRootDir, '..', 'web', $this->uploadPath, $options['format']));
94
95
        if (!is_dir($directory)) {
96
            mkdir($directory, 0755, true);
97
        }
98
99
        $filename = $this->generateName($media->getFile(), $directory);
100
101
        $media->getFile()->move($directory, $filename);
102
103
        if ($media->getId()) {
104
            if (!$options['keep_existing']) {
105
                $this->delete($media);
106
                $entity = $media;
107
            } else {
108
                $entity = new $options['data_class'];
109
            }
110
        } else {
111
            $entity = new $options['data_class'];
112
        }
113
114
        $entity->setFilename($filename);
115
        $entity->setFormat($options['format']);
116
        $entity->setMetadata($this->updateMetaData($media->getFile()));
117
118
        $this->entityManager->persist($entity);
119
        $this->entityManager->flush();
120
121
        return $entity;
122
    }
123
124
    /**
125
     * 
126
     * @param Media $media
127
     * @param array $options
128
     * @param boolean $force
129
     */
130
    public function delete(Media $media, array $options = [], $force = false)
131
    {
132
133
        $directory = implode(DIRECTORY_SEPARATOR, array($this->kernelRootDir, '..', 'web', $this->uploadPath, $options['format']));
134
135
        if (file_exists($directory . DIRECTORY_SEPARATOR . $media->getFilename())) {
136
            unlink($directory . DIRECTORY_SEPARATOR . $media->getFilename());
137
        }
138
        
139
        $this->entityManager->remove($media);
140
        if ($force) {
141
            $this->entityManager->flush();
142
        }
143
    }
144
145
}
146