Completed
Push — master ( 13dd2e...a1a590 )
by Julito
20:58
created

UpdateResourceNodeFileAction::__invoke()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 54
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 26
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 54
rs 9.1928

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\Api;
6
7
use Chamilo\CourseBundle\Entity\CDocument;
8
use Chamilo\CourseBundle\Repository\CDocumentRepository;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
12
class UpdateResourceNodeFileAction
13
{
14
    public function __invoke(CDocument $document, Request $request, CDocumentRepository $repo, EntityManagerInterface $em): CDocument
15
    {
16
        $fileType = $document->getFileType();
17
        $contentData = $request->getContent();
18
        error_log('__invoke');
19
20
        if (!empty($contentData)) {
21
            $contentData = json_decode($contentData, true);
22
            $title = $contentData['title'];
23
            $content = $contentData['contentFile'];
24
            $comment = $contentData['comment'];
25
        } else {
26
            $title = $request->get('title');
27
            $content = $request->request->get('contentFile');
28
            $comment = $request->request->get('comment');
29
        }
30
        //$comment = time();
31
        $document->setTitle($title);
32
        if ('file' === $fileType && !empty($content)) {
33
            $resourceNode = $document->getResourceNode();
34
35
            error_log('to update');
36
            //$document->setUploadFile($file);
37
            //$repo->updateResourceFileContent($document, $content);
38
            if ($resourceNode->hasResourceFile()) {
39
                $resourceNode->setContent($content);
40
                $resourceNode->getResourceFile()->setSize(strlen($content));
41
            }
42
            $resourceNode->setUpdatedAt(new \DateTime());
43
            $resourceNode->getResourceFile()->setUpdatedAt(new \DateTime());
44
            $document->setResourceNode($resourceNode);
45
        }
46
47
        /*if ($request->request->has('resourceLinkList')) {
48
            $links = $request->get('resourceLinkList');
49
            if (false === strpos($links, '[')) {
50
                $links = json_decode('['.$links.']', true);
51
            } else {
52
                $links = json_decode($links, true);
53
            }
54
            if (empty($links)) {
55
                throw new \InvalidArgumentException(
56
                    'resourceLinkList is not a valid json. Example: [{"c_id":1:"visibility":1}]'
57
                );
58
            }
59
            $document->setResourceLinkList($links);
60
        }*/
61
62
        $repo->setResourceTitle($document, $title);
63
        $document->setComment($comment);
64
65
        error_log('Finish update resource node file action');
66
67
        return $document;
68
    }
69
}
70