Passed
Push — master ( 72f3da...dcd5ef )
by Yannick
09:41
created

AddVariantResourceFileAction::__invoke()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 51
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 51
rs 8.1475

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
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Chamilo\CoreBundle\Entity\ResourceFile;
10
use Chamilo\CoreBundle\Entity\ResourceNode;
11
use Chamilo\CoreBundle\Entity\AccessUrl;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
17
class AddVariantResourceFileAction
18
{
19
    public function __invoke(Request $request, EntityManagerInterface $em): ResourceFile
20
    {
21
        $uploadedFile = $request->files->get('file');
22
        if (!$uploadedFile) {
23
            throw new BadRequestHttpException('"file" is required');
24
        }
25
26
        $resourceNodeId = $request->get('resourceNodeId');
27
        if (!$resourceNodeId) {
28
            throw new BadRequestHttpException('"resourceNodeId" is required');
29
        }
30
31
        $resourceNode = $em->getRepository(ResourceNode::class)->find($resourceNodeId);
32
        if (!$resourceNode) {
33
            throw new NotFoundHttpException('ResourceNode not found');
34
        }
35
36
        $accessUrlId = $request->get('accessUrlId');
37
        $accessUrl = null;
38
        if ($accessUrlId) {
39
            $accessUrl = $em->getRepository(AccessUrl::class)->find($accessUrlId);
40
            if (!$accessUrl) {
41
                throw new NotFoundHttpException('AccessUrl not found');
42
            }
43
        }
44
45
        $existingResourceFile = $em->getRepository(ResourceFile::class)->findOneBy([
46
            'resourceNode' => $resourceNode,
47
            'accessUrl' => $accessUrl,
48
        ]);
49
50
        if ($existingResourceFile) {
51
            $existingResourceFile->setTitle($uploadedFile->getClientOriginalName());
52
            $existingResourceFile->setFile($uploadedFile);
53
            $existingResourceFile->setUpdatedAt(\DateTime::createFromImmutable(new \DateTimeImmutable()));
54
            $resourceFile = $existingResourceFile;
55
        } else {
56
            $resourceFile = new ResourceFile();
57
            $resourceFile->setTitle($uploadedFile->getClientOriginalName());
58
            $resourceFile->setFile($uploadedFile);
59
            $resourceFile->setResourceNode($resourceNode);
60
61
            if ($accessUrl) {
62
                $resourceFile->setAccessUrl($accessUrl);
63
            }
64
        }
65
66
        $em->persist($resourceFile);
67
        $em->flush();
68
69
        return $resourceFile;
70
    }
71
}
72