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