Conditions | 8 |
Paths | 10 |
Total Lines | 51 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
19 | public function __invoke(Request $request, EntityManagerInterface $em): ResourceFile |
||
20 | { |
||
21 | $uploadedFile = $request->files->get('file'); |
||
22 | if (!$uploadedFile) { |
||
23 | throw new BadRequestHttpException('"file" is required'); |
||
24 | } |
||
25 | |||
26 | $resourceNodeId = $request->get('resourceNodeId'); |
||
27 | if (!$resourceNodeId) { |
||
28 | throw new BadRequestHttpException('"resourceNodeId" is required'); |
||
29 | } |
||
30 | |||
31 | $resourceNode = $em->getRepository(ResourceNode::class)->find($resourceNodeId); |
||
32 | if (!$resourceNode) { |
||
33 | throw new NotFoundHttpException('ResourceNode not found'); |
||
34 | } |
||
35 | |||
36 | $accessUrlId = $request->get('accessUrlId'); |
||
37 | $accessUrl = null; |
||
38 | if ($accessUrlId) { |
||
39 | $accessUrl = $em->getRepository(AccessUrl::class)->find($accessUrlId); |
||
40 | if (!$accessUrl) { |
||
41 | throw new NotFoundHttpException('AccessUrl not found'); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | $existingResourceFile = $em->getRepository(ResourceFile::class)->findOneBy([ |
||
46 | 'resourceNode' => $resourceNode, |
||
47 | 'accessUrl' => $accessUrl, |
||
48 | ]); |
||
49 | |||
50 | if ($existingResourceFile) { |
||
51 | $existingResourceFile->setTitle($uploadedFile->getClientOriginalName()); |
||
52 | $existingResourceFile->setFile($uploadedFile); |
||
53 | $existingResourceFile->setUpdatedAt(\DateTime::createFromImmutable(new \DateTimeImmutable())); |
||
54 | $resourceFile = $existingResourceFile; |
||
55 | } else { |
||
56 | $resourceFile = new ResourceFile(); |
||
57 | $resourceFile->setTitle($uploadedFile->getClientOriginalName()); |
||
58 | $resourceFile->setFile($uploadedFile); |
||
59 | $resourceFile->setResourceNode($resourceNode); |
||
60 | |||
61 | if ($accessUrl) { |
||
62 | $resourceFile->setAccessUrl($accessUrl); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | $em->persist($resourceFile); |
||
67 | $em->flush(); |
||
68 | |||
69 | return $resourceFile; |
||
70 | } |
||
72 |