Conditions | 12 |
Paths | 291 |
Total Lines | 82 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 13 | ||
Bugs | 4 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
47 | public function upload(File $file, $dest_folder = '', \DateTime $lifetime = null) |
||
48 | { |
||
49 | //preparing dir name |
||
50 | $dest_folder = date('Ymd').'/'.date('G').'/'.$dest_folder; |
||
51 | |||
52 | //checking mimetypes |
||
53 | $mimeTypePassed = false; |
||
54 | foreach ($this->allowedMimetypes as $mimeType) { |
||
55 | if (preg_match('@'.$mimeType.'@', $file->getMimeType())) { |
||
56 | $mimeTypePassed = true; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if (!$mimeTypePassed) { |
||
61 | throw new InvalidMimeTypeException('Only following filetypes are allowed : '.implode(', ', $this->allowedMimetypes)); |
||
62 | } |
||
63 | |||
64 | $fs = new Filesystem(); |
||
65 | if (!$fs->exists($this->uploadDir.$dest_folder)) { |
||
66 | $fs->mkdir($this->uploadDir.$dest_folder); |
||
67 | } |
||
68 | |||
69 | $em = $this->entityManager; |
||
70 | $media = new Media(); |
||
71 | $media->setMime($file->getMimeType()); |
||
72 | |||
73 | // Sanitizing the filename |
||
74 | $slugify = new Slugify(); |
||
75 | if ($file instanceof UploadedFile) { |
||
76 | $filename = $slugify->slugify($file->getClientOriginalName()); |
||
77 | } else { |
||
78 | $filename = $slugify->slugify($file->getFilename()); |
||
79 | } |
||
80 | |||
81 | // A media can have a lifetime and will be deleted with the cleanup function |
||
82 | if (!empty($lifetime)) { |
||
83 | $media->setLifetime($lifetime); |
||
84 | } |
||
85 | |||
86 | // Checking for a media with the same name |
||
87 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($dest_folder.$filename); |
||
88 | $mediaExists = (count($mediaExists) > 0); |
||
89 | if ($mediaExists === false) { |
||
90 | $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
||
91 | } |
||
92 | |||
93 | // If there's one, we try to generate a new name |
||
94 | $extension = $file->getExtension(); |
||
95 | if (empty($extension)) { |
||
96 | $extension = $file->guessExtension(); |
||
97 | } |
||
98 | |||
99 | if ($mediaExists === true){ |
||
100 | $filename = basename($filename, '.'.$extension); |
||
101 | |||
102 | $i = 1; |
||
103 | do { |
||
104 | $media->setName($filename.'-'.$i++.'.'.$extension); |
||
105 | $media->setUri($dest_folder.$media->getName()); |
||
106 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri()); |
||
107 | $mediaExists = (count($mediaExists) > 0); |
||
108 | if ($mediaExists === false) { |
||
109 | $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
||
110 | } |
||
111 | } while ($mediaExists === true); |
||
112 | } else { |
||
113 | $media->setName($filename.'.'.$extension); |
||
114 | $media->setUri($dest_folder.$media->getName()); |
||
115 | } |
||
116 | |||
117 | $file->move($this->uploadDir.$dest_folder, $media->getName()); |
||
118 | chmod($this->uploadDir.$dest_folder.$media->getName(), 0664); |
||
119 | |||
120 | // Getting the salt defined in parameters.yml |
||
121 | $secret = $this->container->getParameter('secret'); |
||
122 | $media->setSecretKey(hash('sha256', $secret.$media->getName().$media->getUri())); |
||
123 | |||
124 | $em->persist($media); |
||
125 | $em->flush(); |
||
126 | |||
127 | return $media; |
||
128 | } |
||
129 | |||
262 |