Conditions | 11 |
Paths | 195 |
Total Lines | 77 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
Bugs | 2 | 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 |
||
46 | public function upload(File $file, $dest_folder = '', \DateTime $lifetime = null) |
||
47 | { |
||
48 | //preparing dir name |
||
49 | $dest_folder = date('Ymd').'/'.date('G').'/'.$dest_folder; |
||
50 | |||
51 | //checking mimetypes |
||
52 | $mimeTypePassed = false; |
||
53 | foreach ($this->allowedMimetypes as $mimeType) { |
||
54 | if (preg_match('@'.$mimeType.'@', $file->getMimeType())) { |
||
55 | $mimeTypePassed = true; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | if (!$mimeTypePassed) { |
||
60 | throw new InvalidMimeTypeException('Only following filetypes are allowed : '.implode(', ', $this->allowedMimetypes)); |
||
61 | } |
||
62 | |||
63 | $fs = new Filesystem(); |
||
64 | if (!$fs->exists($this->uploadDir.$dest_folder)) { |
||
65 | $fs->mkdir($this->uploadDir.$dest_folder); |
||
66 | } |
||
67 | |||
68 | $em = $this->entityManager; |
||
69 | $media = new Media(); |
||
70 | $media->setMime($file->getMimeType()); |
||
71 | |||
72 | // Sanitizing the filename |
||
73 | $slugify = new Slugify(); |
||
74 | if($file instanceof UploadedFile) { |
||
75 | $filename = $slugify->slugify($file->getClientOriginalName()); |
||
76 | } else { |
||
77 | $filename = $slugify->slugify($file->getFilename()); |
||
78 | } |
||
79 | |||
80 | // A media can have a lifetime and will be deleted with the cleanup function |
||
81 | if (!empty($lifetime)) { |
||
82 | $media->setLifetime($lifetime); |
||
83 | } |
||
84 | |||
85 | // Checking for a media with the same name |
||
86 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($dest_folder.$filename); |
||
87 | if (count($mediaExists) === 0) { |
||
88 | $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
||
89 | } |
||
90 | |||
91 | // If there's one, we try to generate a new name |
||
92 | $extension = $file->getExtension(); |
||
93 | if (empty($extension)) { |
||
94 | $extension = $file->guessExtension(); |
||
95 | } |
||
96 | |||
97 | if (count($mediaExists) > 0) { |
||
98 | $filename = basename($filename, '.'.$extension); |
||
99 | |||
100 | $i = 1; |
||
101 | do { |
||
102 | $media->setName($filename.'-'.$i++.'.'.$extension); |
||
103 | $media->setUri($dest_folder.$media->getName()); |
||
104 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri()); |
||
105 | } while (count($mediaExists) > 0); |
||
106 | } else { |
||
107 | $media->setName($filename.'.'.$extension); |
||
108 | $media->setUri($dest_folder.$media->getName()); |
||
109 | } |
||
110 | |||
111 | $file->move($this->uploadDir.$dest_folder, $media->getName()); |
||
112 | chmod($this->uploadDir.$dest_folder.$media->getName(), 0664); |
||
113 | |||
114 | // Getting the salt defined in parameters.yml |
||
115 | $secret = $this->container->getParameter('secret'); |
||
116 | $media->setSecretKey(hash('sha256', $secret.$media->getName().$media->getUri())); |
||
117 | |||
118 | $em->persist($media); |
||
119 | $em->flush(); |
||
120 | |||
121 | return $media; |
||
122 | } |
||
123 | |||
251 |