PresentationController::get()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 5
nop 1
dl 0
loc 39
rs 9.2248
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\File;
4
5
use App\Entity\File;
6
use App\Service\FileService;
7
use Ds\Component\Acl\Model\Permission;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Acl\Model\Permission 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 Ds\Component\Acl\Voter\PropertyVoter;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Acl\Voter\PropertyVoter 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 LogicException;
10
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\RequestStack 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 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...
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...n\NotFoundHttpException 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
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface 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
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...n\AccessDeniedException 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...
15
16
use ApiPlatform\Core\Annotation\ApiResource;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Core\Annotation\ApiResource 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...
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
0 ignored issues
show
Bug introduced by
The type Sensio\Bundle\FrameworkE...\Configuration\Security 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...
18
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...
19
20
/**
21
 * Class PresentationController
22
 *
23
 * @ApiResource
24
 */
25
final class PresentationController
26
{
27
    /**
28
     * @var \Symfony\Component\HttpFoundation\RequestStack
29
     */
30
    private $requestStack;
31
32
    /**
33
     * @var \App\Service\FileService
34
     */
35
    private $fileService;
36
37
    /**
38
     * @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
39
     */
40
    private $tokenStorage;
41
42
    /**
43
     * @var \Ds\Component\Acl\Voter\PropertyVoter
44
     */
45
    private $propertyVoter;
46
47
    /**
48
     * Constructor
49
     *
50
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
51
     * @param \App\Service\FileService $fileService
52
     * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokenStorage
53
     * @param \Ds\Component\Acl\Voter\PropertyVoter $propertyVoter
54
     */
55
    public function __construct(RequestStack $requestStack, FileService $fileService, TokenStorageInterface $tokenStorage, PropertyVoter $propertyVoter)
56
    {
57
        $this->requestStack = $requestStack;
58
        $this->fileService = $fileService;
59
        $this->tokenStorage = $tokenStorage;
60
        $this->propertyVoter = $propertyVoter;
61
    }
62
63
    /**
64
     * Presentation
65
     *
66
     * @Route(path="/files/{slug}/presentation", methods={"GET"})
67
     */
68
    public function get($slug)
69
    {
70
        $request = $this->requestStack->getCurrentRequest();
71
        $file = $this->fileService->getRepository()->findOneBy(['slug' => $slug]);
72
73
        if (!$file) {
74
            throw new NotFoundHttpException('File not found.');
75
        }
76
77
        $token = $this->tokenStorage->getToken();
78
        $subject = new Subject;
0 ignored issues
show
Bug introduced by
The type App\Controller\File\Subject 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...
79
        $subject
80
            ->setType(Permission::PROPERTY)
81
            ->setValue(File::class.'.presentation')
82
            ->setEntity($file->getOwner())
83
            ->setEntityUuid($file->getOwnerUuid());
84
85
        $vote = $this->propertyVoter->vote($token, $subject, [Permission::READ]);
86
87
        if (PropertyVoter::ACCESS_ABSTAIN === $vote) {
88
            throw new LogicException('Voter cannot abstain from voting.');
89
        }
90
91
        if (PropertyVoter::ACCESS_GRANTED !== $vote) {
92
            throw new AccessDeniedException('Access denied.');
93
        }
94
95
        $presentation = $file->getPresentation();
96
        $locale = $request->query->get('locale', null);
97
98
        if (!array_key_exists($locale, $presentation)) {
99
            throw new NotFoundHttpException('File locale not found.');
100
        }
101
102
        $presentation = base64_decode($presentation[$locale]);
103
        $type = $file->getType();
104
        $response = new Response($presentation, Response::HTTP_OK, ['Content-Type' => $type]);
105
106
        return $response;
107
    }
108
}
109