Completed
Push — master ( a1a590...8f8784 )
by Julito
18:16 queued 16s
created

CreateResourceNodeFileAction::__invoke()   D

Complexity

Conditions 14
Paths 280

Size

Total Lines 93
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 60
c 1
b 0
f 0
nc 280
nop 2
dl 0
loc 93
rs 4.4333

How to fix   Long Method    Complexity   

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 Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
13
class CreateResourceNodeFileAction
14
{
15
    public function __invoke(Request $request, CDocumentRepository $repo): CDocument
16
    {
17
        error_log('CreateResourceNodeFileAction __invoke');
18
        $document = new CDocument();
19
20
        $contentData = $request->getContent();
21
        error_log('CreateResourceNodeFileAction __invoke');
22
23
        if (!empty($contentData)) {
24
            $contentData = json_decode($contentData, true);
25
            error_log(print_r($contentData, 1));
26
            $title = $contentData['title'];
27
            $content = $contentData['contentFile'];
28
            $comment = $contentData['comment'];
29
        } else {
30
            $title = $request->get('title');
31
            $content = $request->request->get('contentFile');
32
            $comment = $request->request->get('comment');
33
        }
34
35
        if ($request->request->has('filetype')) {
36
            $document->setFiletype($request->get('filetype'));
37
        }
38
39
        $content = '';
40
        if ($request->request->has('contentFile')) {
41
            $document->setFiletype('file');
42
            $content = $request->request->get('contentFile');
43
        }
44
45
        $nodeId = (int) $request->get('parentResourceNodeId');
46
        $document->setParentResourceNode($nodeId);
47
48
        switch ($document->getFiletype()) {
49
            case 'file':
50
                $fileParsed = false;
51
                // File upload
52
                if ($request->files->count() > 0) {
53
                    /** @var UploadedFile $uploadedFile */
54
                    $uploadedFile = $request->files->get('uploadFile');
55
                    if (!$uploadedFile) {
0 ignored issues
show
introduced by
$uploadedFile is of type Symfony\Component\HttpFoundation\File\UploadedFile, thus it always evaluated to true.
Loading history...
56
                        throw new BadRequestHttpException('"uploadFile" is required');
57
                    }
58
                    $title = $uploadedFile->getClientOriginalName();
59
60
                    $document->setTitle($title);
61
                    $document->setUploadFile($uploadedFile);
62
                    $fileParsed = true;
63
                }
64
65
                // Get data in content and create a HTML file
66
                if (false === $fileParsed && $content) {
67
                    $document->setTitle($title);
68
69
                    $title .= '.html';
70
                    $handle = tmpfile();
71
                    fwrite($handle, $content);
72
                    $meta = stream_get_meta_data($handle);
73
                    $file = new UploadedFile($meta['uri'], $title, 'text/html', null, true);
74
                    $document->setUploadFile($file);
75
                    $fileParsed = true;
76
                }
77
78
                if (false === $fileParsed) {
79
                    throw new \InvalidArgumentException('filetype was set to "file" but not upload found');
80
                }
81
82
                break;
83
84
            case 'folder':
85
                $document->setTitle($title);
86
87
                break;
88
        }
89
90
        if ($request->request->has('resourceLinkList')) {
91
            $links = $request->get('resourceLinkList');
92
            if (false === strpos($links, '[')) {
93
                $links = json_decode('['.$links.']', true);
94
            } else {
95
                $links = json_decode($links, true);
96
            }
97
            if (empty($links)) {
98
                throw new \InvalidArgumentException(
99
                    'resourceLinkList is not a valid json. Example: [{"c_id":1:"visibility":1}]'
100
                );
101
            }
102
            $document->setResourceLinkList($links);
103
        }
104
105
        $document->setComment($comment);
106
107
        return $document;
108
    }
109
}
110