Passed
Push — master ( 597eeb...295a68 )
by Luiz Kim
02:21
created

FileUploadController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...BadRequestHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use ControleOnline\Entity\File;
9
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\Routing\Annotation\Route;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Routing\Annotation\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use ControleOnline\Service\HydratorService;
12
use Exception;
13
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class FileUploadController
16
{
17
18
    public function __construct(
19
        private EntityManagerInterface $em,
20
        private HydratorService $hydratorService
21
    ) {}
22
23
    public function __invoke(Request $request): Response
24
    {
25
26
        try {
27
            $file = $request->files->get('file');
28
29
            if (!$file) {
30
                throw new BadRequestHttpException('No file provided');
31
            }
32
33
            $content = file_get_contents($file->getPathname());
34
            $fileType = $file->getClientMimeType();
35
            $originalFilename = $file->getClientOriginalName();
0 ignored issues
show
Unused Code introduced by
The assignment to $originalFilename is dead and can be removed.
Loading history...
36
37
            $fileEntity = new File();
38
            $fileEntity->setContent($content);
39
            $fileEntity->setFileType($fileType);
40
41
            $this->em->persist($fileEntity);
42
            $this->em->flush();
43
            return new JsonResponse($this->hydratorService->item(File::class, $fileEntity->getId(), 'file_read'));
44
        } catch (Exception $e) {
45
            return new JsonResponse($this->hydratorService->error($e));
46
        }
47
    }
48
}
49