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

CreateResourceNodeFileAction::__invoke()   C

Complexity

Conditions 13
Paths 140

Size

Total Lines 79
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 49
c 0
b 0
f 0
nc 140
nop 2
dl 0
loc 79
rs 6.2833

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
        $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) {
0 ignored issues
show
introduced by
$uploadedFile is of type Symfony\Component\HttpFoundation\File\UploadedFile, thus it always evaluated to true.
Loading history...
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
    }
95
}
96