| Conditions | 5 |
| Paths | 5 |
| Total Lines | 73 |
| Code Lines | 31 |
| 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 |
||
| 15 | public function __invoke(Request $request, CDocumentRepository $repo): CDocument |
||
| 16 | { |
||
| 17 | error_log('__invoke update'); |
||
| 18 | |||
| 19 | $id = (int) $request->get('id'); |
||
| 20 | error_log($id); |
||
| 21 | if (0 === $id) { |
||
| 22 | throw new \InvalidArgumentException('Not valid id'); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** @var CDocument $document */ |
||
| 26 | $document = $repo->find($id); |
||
| 27 | |||
| 28 | if (null === $document) { |
||
| 29 | throw new \InvalidArgumentException("Resource $id not found"); |
||
| 30 | } |
||
| 31 | |||
| 32 | error_log($request->getMethod()); |
||
| 33 | $fileType = $document->getFileType(); |
||
| 34 | |||
| 35 | $data = $request->getContent(); |
||
| 36 | $title = $request->get('title'); |
||
| 37 | error_log($title); |
||
| 38 | $title = $request->request->get('title'); |
||
| 39 | |||
| 40 | error_log($title); |
||
| 41 | |||
| 42 | $comment = $request->request->get('comment'); |
||
| 43 | |||
| 44 | error_log($comment); |
||
| 45 | |||
| 46 | if (empty($data)) { |
||
| 47 | throw new \InvalidArgumentException("No data"); |
||
| 48 | } |
||
| 49 | |||
| 50 | |||
| 51 | error_log(print_r($data, 1)); |
||
| 52 | $data = json_decode($data, true); |
||
| 53 | error_log(print_r($data, 1)); |
||
| 54 | |||
| 55 | $title = $data['title']; |
||
| 56 | $content = $data['contentFile']; |
||
| 57 | $comment = $data['comment']; |
||
| 58 | //$nodeId = (int) $request->get('parentResourceNodeId'); |
||
| 59 | //$document->setParentResourceNode($nodeId); |
||
| 60 | $document->setTitle($title); |
||
| 61 | |||
| 62 | if ('file' === $fileType) { |
||
| 63 | $repo->updateResourceFileContent($document, $content); |
||
| 64 | } |
||
| 65 | |||
| 66 | /*if ($request->request->has('resourceLinkList')) { |
||
| 67 | $links = $request->get('resourceLinkList'); |
||
| 68 | if (false === strpos($links, '[')) { |
||
| 69 | $links = json_decode('['.$links.']', true); |
||
| 70 | } else { |
||
| 71 | $links = json_decode($links, true); |
||
| 72 | } |
||
| 73 | if (empty($links)) { |
||
| 74 | throw new \InvalidArgumentException( |
||
| 75 | 'resourceLinkList is not a valid json. Example: [{"c_id":1:"visibility":1}]' |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | $document->setResourceLinkList($links); |
||
| 79 | }*/ |
||
| 80 | |||
| 81 | $repo->setResourceTitle($document, $title); |
||
| 82 | |||
| 83 | //$document->setTitle($title); |
||
| 84 | $document->setComment($comment); |
||
| 85 | //$document->getResourceNode()->setTitle($title); |
||
| 86 | |||
| 87 | return $document; |
||
| 88 | } |
||
| 90 |