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