Completed
Push — master ( 83f4f4...7038dd )
by Julito
11:28
created

UpdateResourceNodeFileAction::__invoke()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 73
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 31
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 73
rs 9.1128

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CourseBundle\Entity\CDocument;
8
use Chamilo\CourseBundle\Repository\CDocumentRepository;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
13
class UpdateResourceNodeFileAction
14
{
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
    }
89
}
90