Passed
Push — master ( b34284...653853 )
by Angel Fernando Quiroz
12:36
created

ControllerTrait::setHeadersToStreamedResponse()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 1
nop 9
dl 0
loc 23
rs 9.9332

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Traits;
8
9
use Chamilo\CoreBundle\Entity\AccessUrl;
10
use Chamilo\CoreBundle\Helpers\GlideHelper;
11
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
12
use Chamilo\CoreBundle\Repository\Node\MessageAttachmentRepository;
13
use Chamilo\CoreBundle\Repository\ResourceFactory;
14
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
15
use Chamilo\CoreBundle\Settings\SettingsManager;
16
use Chamilo\CourseBundle\Repository\CAnnouncementAttachmentRepository;
17
use Chamilo\CourseBundle\Repository\CAnnouncementRepository;
18
use Chamilo\CourseBundle\Repository\CAttendanceRepository;
19
use Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository;
20
use Chamilo\CourseBundle\Repository\CDocumentRepository;
21
use Chamilo\CourseBundle\Repository\CForumAttachmentRepository;
22
use Chamilo\CourseBundle\Repository\CForumRepository;
23
use Chamilo\CourseBundle\Repository\CLpCategoryRepository;
24
use Chamilo\CourseBundle\Repository\CLpRepository;
25
use Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository;
26
use Chamilo\CourseBundle\Repository\CQuizQuestionRepository;
27
use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository;
28
use Chamilo\CourseBundle\Repository\CStudentPublicationCorrectionRepository;
29
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
30
use Chamilo\CourseBundle\Repository\CToolRepository;
31
use Chamilo\LtiBundle\Repository\ExternalToolRepository;
32
use Psr\Container\ContainerExceptionInterface;
33
use Psr\Container\ContainerInterface;
34
use Psr\Container\NotFoundExceptionInterface;
35
use Sylius\Bundle\SettingsBundle\Form\Factory\SettingsFormFactory;
36
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
37
use Symfony\Component\HttpFoundation\Request;
38
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
39
use Symfony\Contracts\Translation\TranslatorInterface;
40
41
trait ControllerTrait
42
{
43
    /**
44
     * @var ContainerInterface
45
     */
46
    protected $container;
47
48
    public static function getSubscribedServices(): array
49
    {
50
        $services = AbstractController::getSubscribedServices();
51
        $services['translator'] = TranslatorInterface::class;
52
        $services['glide'] = GlideHelper::class;
53
        // $services['chamilo_settings.form_factory.settings'] = SettingsFormFactory::class;
54
55
        $services[] = SettingsManager::class;
56
        $services[] = MessageAttachmentRepository::class;
57
        $services[] = ResourceFactory::class;
58
        $services[] = ResourceNodeRepository::class;
59
        $services[] = SettingsFormFactory::class;
60
61
        /*
62
            The following classes are needed in order to load the resources files when using the /r/ path
63
            For example: http://my.chamilomaster.net/r/agenda/event_attachments/96/download?cid=1&sid=0&gid=0
64
            Then the repository CCalendarEventAttachmentRepository need to be added here,
65
            because it was set in the tools.yml like this:
66
            chamilo_core.tool.agenda:
67
                (...)
68
                event_attachments:
69
                    repository: Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository
70
        */
71
        $services[] = CAnnouncementRepository::class;
72
        $services[] = CAnnouncementAttachmentRepository::class;
73
        $services[] = CAttendanceRepository::class;
74
        $services[] = CCalendarEventAttachmentRepository::class;
75
        $services[] = CDocumentRepository::class;
76
        $services[] = CForumRepository::class;
77
        $services[] = CForumAttachmentRepository::class;
78
        $services[] = CLpRepository::class;
79
        $services[] = CLpCategoryRepository::class;
80
        $services[] = CToolRepository::class;
81
        $services[] = CQuizQuestionRepository::class;
82
        $services[] = CQuizQuestionCategoryRepository::class;
83
        $services[] = CStudentPublicationRepository::class;
84
        $services[] = CStudentPublicationCommentRepository::class;
85
        $services[] = CStudentPublicationCorrectionRepository::class;
86
        $services[] = ExternalToolRepository::class;
87
        $services[] = IllustrationRepository::class;
88
89
        return $services;
90
    }
91
92
    public function getRequest(): ?Request
93
    {
94
        return $this->container->get('request_stack')->getCurrentRequest();
95
    }
96
97
    public function abort(string $message = ''): void
98
    {
99
        throw new NotFoundHttpException($message);
100
    }
101
102
    /**
103
     * Translator shortcut.
104
     */
105
    public function trans(string $variable): string
106
    {
107
        /** @var TranslatorInterface $translator */
108
        $translator = $this->container->get('translator');
109
110
        return $translator->trans($variable);
111
    }
112
113
    /**
114
     * @throws ContainerExceptionInterface
115
     * @throws NotFoundExceptionInterface
116
     */
117
    public function getGlide(): GlideHelper
118
    {
119
        return $this->container->get('glide');
120
    }
121
122
    public function getAccessUrl(): ?AccessUrl
123
    {
124
        $urlId = $this->getRequest()->getSession()->get('access_url_id');
125
126
        return $this->container->get('doctrine')->getRepository(AccessUrl::class)->find($urlId);
127
    }
128
129
    protected function getSettingsManager(): SettingsManager
130
    {
131
        return $this->container->get(SettingsManager::class);
132
    }
133
134
    protected function getSettingsFormFactory()
135
    {
136
        return $this->container->get(SettingsFormFactory::class);
137
    }
138
139
    /**
140
     * @return array<int, int>
141
     */
142
    protected function getRange(Request $request, int $fileSize): array
143
    {
144
        $range = $request->headers->get('Range');
145
146
        if ($range) {
147
            [, $range] = explode('=', $range, 2);
148
            [$start, $end] = explode('-', $range);
149
150
            $start = (int) $start;
151
            $end = ('' === $end) ? $fileSize - 1 : (int) $end;
152
153
            $length = $end - $start + 1;
154
        } else {
155
            $start = 0;
156
            $end = $fileSize - 1;
157
            $length = $fileSize;
158
        }
159
160
        return [$start, $end, $length];
161
    }
162
163
    /**
164
     * @param resource $stream
165
     */
166
    protected function echoBuffer($stream, int $start, int $length): void
167
    {
168
        fseek($stream, $start);
169
170
        $bytesSent = 0;
171
172
        while ($bytesSent < $length && !feof($stream)) {
173
            $buffer = fread($stream, min(1024 * 8, $length - $bytesSent));
174
175
            echo $buffer;
176
177
            $bytesSent += \strlen($buffer);
178
        }
179
180
        fclose($stream);
181
    }
182
}
183