Passed
Push — master ( d5b010...83f4f4 )
by Julito
11:40
created

CreateResourceNodeFileAction::__invoke()   B

Complexity

Conditions 10
Paths 30

Size

Total Lines 60
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 38
c 1
b 0
f 0
nc 30
nop 1
dl 0
loc 60
rs 7.6666

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;
6
7
use Chamilo\CourseBundle\Entity\CDocument;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
11
12
class CreateResourceNodeFileAction
13
{
14
    public function __invoke(Request $request): CDocument
15
    {
16
        $document = new CDocument();
17
        $title = $request->get('title');
18
19
        if ('file' === $request->get('filetype')) {
20
            $fileParsed = false;
21
            // File upload
22
            if ($request->files->count() > 0) {
23
                /** @var UploadedFile $uploadedFile */
24
                $uploadedFile = $request->files->get('uploadFile');
25
                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...
26
                    throw new BadRequestHttpException('"uploadFile" is required');
27
                }
28
                $title = $uploadedFile->getClientOriginalName();
29
                $document->setUploadFile($uploadedFile);
30
                $fileParsed = true;
31
            }
32
33
            // Get data in content and create a HTML file
34
            if (false === $fileParsed && $request->request->has('content')) {
35
                $content = $request->request->get('content');
36
                $title .= '.html';
37
                $handle = tmpfile();
38
                fwrite($handle, $content);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
                fwrite(/** @scrutinizer ignore-type */ $handle, $content);
Loading history...
39
                $meta = stream_get_meta_data($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $stream of stream_get_meta_data() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
                $meta = stream_get_meta_data(/** @scrutinizer ignore-type */ $handle);
Loading history...
40
                $file = new UploadedFile($meta['uri'], $title, 'text/html', null, true);
41
                $document->setUploadFile($file);
42
                $fileParsed = true;
43
            }
44
45
            if (false === $fileParsed) {
46
                throw new \InvalidArgumentException(
47
                    'filetype was set to "file" but not upload found'
48
                );
49
            }
50
        }
51
52
        if ($request->request->has('resourceLinkList')) {
53
            $links = $request->get('resourceLinkList');
54
            if (strpos($links, '[') === false) {
55
                $links = json_decode('['.$links.']', true);
56
            } else {
57
                $links = json_decode($links, true);
58
            }
59
            if (empty($links)) {
60
                throw new \InvalidArgumentException(
61
                    'resourceLinkList is not a valid json. Example: [{"c_id":1:"visibility":1}]'
62
                );
63
            }
64
            $document->setResourceLinkList($links);
65
        }
66
67
        $document->setTitle($title);
68
        $document->setComment($request->get('comment'));
69
70
        $nodeId = (int) $request->get('parentResourceNodeId');
71
        $document->setParentResourceNode($nodeId);
72
73
        return $document;
74
    }
75
}
76