GetFileDataAction::__invoke()   A
last analyzed

Complexity

Conditions 5
Paths 19

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 26
c 4
b 0
f 0
dl 0
loc 38
rs 9.1928
cc 5
nc 19
nop 2
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
            $fileType = $file->getFileType();
54
            $ext = $file->getExtension();
55
            if ($ext == 'svg') {
56
                $response->headers->set('Content-Type', 'image/svg+xml');
57
            } else {
58
                $response->headers->set('Content-Type', "$fileType/$ext");
59
            }
60
61
            if ($fileType == 'image')
62
                $disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_INLINE, basename($request->getPathInfo()));
63
            else
64
                $disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, basename($request->getPathInfo()));
65
66
            $response->headers->set('Content-Disposition', $disposition);
67
68
            return $response;
69
        } catch (\Exception $e) {
70
            return new JsonResponse([
71
                'response' => [
72
                    'data'    => [],
73
                    'count'   => 0,
74
                    'error'   => $e->getMessage(),
75
                    'success' => false,
76
                ],
77
            ]);
78
        }
79
    }
80
}
81