Passed
Push — master ( d32fef...808642 )
by Luiz Kim
14:13 queued 11:54
created

GetFileDataAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 27
c 1
b 0
f 0
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 34 4
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use ControleOnline\Entity\File;
6
use Symfony\Component\HttpFoundation\HeaderUtils;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\HeaderUtils 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\HttpFoundation\StreamedResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\StreamedResponse 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 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...
9
use Symfony\Component\HttpKernel\KernelInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKernel\KernelInterface 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 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...
11
12
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...
13
14
15
16
class GetFileDataAction
17
{
18
    /**
19
     * Entity Manager
20
     *
21
     * @var EntityManagerInterface
22
     */
23
    private $manager = null;
24
25
    /**
26
     * Synfony Kernel
27
     *
28
     * @var KernelInterface
29
     */
30
    private $kernel;
31
32
    public function __construct(EntityManagerInterface $entityManager, KernelInterface $appKernel)
33
    {
34
        $this->kernel  = $appKernel;
35
        $this->manager = $entityManager;
36
    }
37
38
    public function __invoke(File $data,  Request $request)
39
    {
40
41
        try {
42
            $file = $data;
43
            //$file = $this->manager->getRepository(File::class)->findOneBy(['url' => $request->getPathInfo()]);
44
            if (!$file)
0 ignored issues
show
introduced by
$file is of type ControleOnline\Entity\File, thus it always evaluated to true.
Loading history...
45
                throw new \Exception('Not found', 404);
46
47
48
            $content  = $file->getContent();
49
            $response = new StreamedResponse(function () use ($content) {
50
                fputs(fopen('php://output', 'wb'), $content);
51
            });
52
53
54
            $ext = $file->getFileType();
55
            if ($ext == 'svg') {
56
                $response->headers->set('Content-Type', 'image/svg+xml');
57
            } else {
58
                $response->headers->set('Content-Type', $ext);
59
            }
60
61
            $disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, basename($request->getPathInfo()));
62
            $response->headers->set('Content-Disposition', $disposition);
63
64
            return $response;
65
        } catch (\Exception $e) {
66
            return new JsonResponse([
67
                'response' => [
68
                    'data'    => [],
69
                    'count'   => 0,
70
                    'error'   => $e->getMessage(),
71
                    'success' => false,
72
                ],
73
            ]);
74
        }
75
    }
76
}
77