Conditions | 10 |
Paths | 99 |
Total Lines | 74 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
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 |
||
43 | public function upload(UploadedFile $file, $dest_folder = '', \DateTime $lifetime = null) |
||
44 | { |
||
45 | //preparing dir name |
||
46 | $dest_folder = date('Ymd').'/'.date('G').'/'.$dest_folder; |
||
47 | |||
48 | //checking mimetypes |
||
49 | $mimeTypePassed = false; |
||
50 | foreach ($this->allowedMimetypes as $mimeType) { |
||
51 | if (preg_match('@'.$mimeType.'@', $file->getMimeType())) { |
||
52 | $mimeTypePassed = true; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | if (!$mimeTypePassed) { |
||
57 | throw new InvalidMimeTypeException('Only following filetypes are allowed : '.implode(', ', $this->allowedMimetypes)); |
||
58 | } |
||
59 | |||
60 | $fs = new Filesystem(); |
||
61 | if (!$fs->exists($this->uploadDir.$dest_folder)) { |
||
62 | $fs->mkdir($this->uploadDir.$dest_folder); |
||
63 | } |
||
64 | |||
65 | $em = $this->entityManager; |
||
66 | $media = new Media(); |
||
67 | $media->setMime($file->getMimeType()); |
||
68 | |||
69 | // Sanitizing the filename |
||
70 | $slugify = new Slugify(); |
||
71 | $filename = $slugify->slugify($file->getClientOriginalName()); |
||
72 | |||
73 | // A media can have a lifetime and will be deleted with the cleanup function |
||
74 | if (!empty($lifetime)) { |
||
75 | $media->setLifetime($lifetime); |
||
76 | } |
||
77 | |||
78 | // Checking for a media with the same name |
||
79 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($dest_folder.$filename); |
||
80 | if (count($mediaExists) === 0) { |
||
81 | $mediaExists = $fs->exists($this->uploadDir.$dest_folder.$filename); |
||
82 | } |
||
83 | |||
84 | // If there's one, we try to generate a new name |
||
85 | $extension = $file->getExtension(); |
||
86 | if (empty($extension)) { |
||
87 | $extension = $file->guessExtension(); |
||
88 | } |
||
89 | |||
90 | if (count($mediaExists) > 0) { |
||
91 | $filename = basename($filename, '.'.$extension); |
||
92 | |||
93 | $i = 1; |
||
94 | do { |
||
95 | $media->setName($filename.'-'.$i++.'.'.$extension); |
||
96 | $media->setUri($dest_folder.$media->getName()); |
||
97 | $mediaExists = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneByUri($media->getUri()); |
||
98 | } while (count($mediaExists) > 0); |
||
99 | } else { |
||
100 | $media->setName($filename.'.'.$extension); |
||
101 | $media->setUri($dest_folder.$media->getName()); |
||
102 | } |
||
103 | |||
104 | $file->move($this->uploadDir.$dest_folder, $media->getName()); |
||
105 | |||
106 | chmod($this->uploadDir.$dest_folder.$media->getName(), 0664); |
||
107 | |||
108 | // Getting the salt defined in parameters.yml |
||
109 | $secret = $this->container->getParameter('secret'); |
||
110 | $media->setSecretKey(hash('sha256', $secret.$media->getName().$media->getUri())); |
||
111 | |||
112 | $em->persist($media); |
||
113 | $em->flush(); |
||
114 | |||
115 | return $media; |
||
116 | } |
||
117 | |||
245 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.