| Conditions | 15 |
| Paths | 328 |
| Total Lines | 88 |
| Code Lines | 56 |
| 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 |
||
| 14 | public function __invoke(Request $request): CDocument |
||
| 15 | { |
||
| 16 | error_log('CreateResourceNodeFileAction __invoke'); |
||
| 17 | $document = new CDocument(); |
||
| 18 | |||
| 19 | $contentData = $request->getContent(); |
||
| 20 | error_log('CreateResourceNodeFileAction __invoke'); |
||
| 21 | |||
| 22 | if (!empty($contentData)) { |
||
| 23 | $contentData = json_decode($contentData, true); |
||
| 24 | error_log(print_r($contentData, 1)); |
||
| 25 | $title = $contentData['title']; |
||
| 26 | $comment = $contentData['comment']; |
||
| 27 | } else { |
||
| 28 | $title = $request->get('title'); |
||
| 29 | $comment = $request->request->get('comment'); |
||
| 30 | } |
||
| 31 | |||
| 32 | if ($request->request->has('filetype')) { |
||
| 33 | $document->setFiletype($request->get('filetype')); |
||
| 34 | } |
||
| 35 | |||
| 36 | $content = ''; |
||
| 37 | if ($request->request->has('contentFile')) { |
||
| 38 | $document->setFiletype('file'); |
||
| 39 | $content = $request->request->get('contentFile'); |
||
| 40 | } |
||
| 41 | |||
| 42 | $nodeId = (int) $request->get('parentResourceNodeId'); |
||
| 43 | $document->setParentResourceNode($nodeId); |
||
| 44 | |||
| 45 | switch ($document->getFiletype()) { |
||
| 46 | case 'file': |
||
| 47 | $fileParsed = false; |
||
| 48 | // File upload |
||
| 49 | if ($request->files->count() > 0) { |
||
| 50 | /** @var UploadedFile $uploadedFile */ |
||
| 51 | $uploadedFile = $request->files->get('uploadFile'); |
||
| 52 | if (!$uploadedFile) { |
||
|
|
|||
| 53 | throw new BadRequestHttpException('"uploadFile" is required'); |
||
| 54 | } |
||
| 55 | $title = $uploadedFile->getClientOriginalName(); |
||
| 56 | $document->setUploadFile($uploadedFile); |
||
| 57 | $fileParsed = true; |
||
| 58 | } |
||
| 59 | |||
| 60 | // Get data in content and create a HTML file |
||
| 61 | if (false === $fileParsed && $content) { |
||
| 62 | $handle = tmpfile(); |
||
| 63 | fwrite($handle, $content); |
||
| 64 | $meta = stream_get_meta_data($handle); |
||
| 65 | $file = new UploadedFile($meta['uri'], $title.'.html', 'text/html', null, true); |
||
| 66 | $document->setUploadFile($file); |
||
| 67 | $fileParsed = true; |
||
| 68 | } |
||
| 69 | |||
| 70 | if (false === $fileParsed) { |
||
| 71 | throw new \InvalidArgumentException('filetype was set to "file" but not upload found'); |
||
| 72 | } |
||
| 73 | |||
| 74 | break; |
||
| 75 | |||
| 76 | case 'folder': |
||
| 77 | break; |
||
| 78 | } |
||
| 79 | |||
| 80 | if (empty($title)) { |
||
| 81 | throw new \InvalidArgumentException('title required'); |
||
| 82 | } |
||
| 83 | |||
| 84 | $document->setTitle($title); |
||
| 85 | |||
| 86 | if ($request->request->has('resourceLinkList')) { |
||
| 87 | $links = $request->get('resourceLinkList'); |
||
| 88 | if (false === strpos($links, '[')) { |
||
| 89 | $links = json_decode('['.$links.']', true); |
||
| 90 | } else { |
||
| 91 | $links = json_decode($links, true); |
||
| 92 | } |
||
| 93 | if (empty($links)) { |
||
| 94 | throw new \InvalidArgumentException('resourceLinkList is not a valid json. Example: [{"c_id":1:"visibility":1}]'); |
||
| 95 | } |
||
| 96 | $document->setResourceLinkList($links); |
||
| 97 | } |
||
| 98 | |||
| 99 | $document->setComment($comment); |
||
| 100 | |||
| 101 | return $document; |
||
| 102 | } |
||
| 104 |