Conditions | 13 |
Paths | 21 |
Total Lines | 76 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
27 | public function __invoke( |
||
28 | Request $request, |
||
29 | EntityManagerInterface $em, |
||
30 | UserHelper $userHelper, |
||
31 | CBlogAttachmentRepository $attachRepo, |
||
32 | ResourceNodeRepository $resourceNodeRepo |
||
33 | |||
34 | ): JsonResponse { |
||
35 | $user = $userHelper->getCurrent(); |
||
36 | if (!$user) { |
||
37 | throw new UnauthorizedHttpException('', 'Unauthorized.'); |
||
38 | } |
||
39 | |||
40 | /** @var UploadedFile|null $file */ |
||
41 | $file = $request->files->get('uploadFile'); |
||
42 | if (!$file) { |
||
43 | foreach ($request->files as $val) { |
||
44 | if ($val instanceof UploadedFile) { $file = $val; break; } |
||
45 | } |
||
46 | } |
||
47 | if (!$file) { |
||
48 | throw new BadRequestHttpException('"uploadFile" is required'); |
||
49 | } |
||
50 | |||
51 | // IRIs |
||
52 | $blogIri = (string) $request->request->get('blog', ''); |
||
53 | $postIri = (string) $request->request->get('post', ''); |
||
54 | if ($blogIri === '' || $postIri === '') { |
||
55 | throw new BadRequestHttpException('Both "blog" and "post" are required IRIs.'); |
||
56 | } |
||
57 | |||
58 | /** @var CBlog|null $blog */ |
||
59 | $blog = $em->getRepository(CBlog::class)->find(self::idFromIri($blogIri)); |
||
60 | /** @var CBlogPost|null $post */ |
||
61 | $post = $em->getRepository(CBlogPost::class)->find(self::idFromIri($postIri)); |
||
62 | if (!$blog || !$post) { |
||
63 | throw new BadRequestHttpException('Invalid blog/post IRI.'); |
||
64 | } |
||
65 | |||
66 | $node = $blog->getResourceNode(); |
||
67 | if (!$node) { |
||
68 | throw new BadRequestHttpException('Blog has no resource node.'); |
||
69 | } |
||
70 | |||
71 | $original = $file->getClientOriginalName() ?: 'upload.bin'; |
||
72 | $filename = $this->uniqueFilenameForAttachments($original, $attachRepo); |
||
73 | |||
74 | $rf = new ResourceFile(); |
||
75 | $rf->setResourceNode($node); |
||
76 | $rf->setTitle($filename); |
||
77 | $rf->setFile($file); |
||
78 | |||
79 | $em->persist($rf); |
||
80 | $em->flush(); |
||
81 | |||
82 | $downloadUrl = $resourceNodeRepo->getResourceFileUrl($node); |
||
83 | |||
84 | $att = new CBlogAttachment(); |
||
85 | $att->setBlog($blog); |
||
86 | $att->setPost($post); |
||
87 | $att->setFilename($filename); |
||
88 | $att->setSize((int) ($rf->getSize() ?? 0)); |
||
89 | $att->setPath($downloadUrl); |
||
90 | $att->setComment($request->request->get('comment') ?: null); |
||
91 | |||
92 | $em->persist($att); |
||
93 | $em->flush(); |
||
94 | |||
95 | return new JsonResponse([ |
||
96 | 'ok' => true, |
||
97 | 'id' => (int) $att->getIid(), |
||
98 | 'filename' => $att->getFilename(), |
||
99 | 'size' => $att->getSize(), |
||
100 | 'path' => $downloadUrl, |
||
101 | 'resourceFileId' => (int) $rf->getId(), |
||
102 | ], 201); |
||
103 | } |
||
124 |