Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SharedStimulusStyling
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 save
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 load
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 getStylesheets
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 loadStylesheet
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 upload
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2021 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoMediaManager\controller;
24
25use oat\taoMediaManager\model\sharedStimulus\css\handler\UploadStylesheetRequestHandler;
26use oat\taoMediaManager\model\sharedStimulus\css\service\UploadStylesheetService;
27use Throwable;
28use tao_helpers_Http as HttpHelper;
29use oat\oatbox\log\LoggerAwareTrait;
30use tao_actions_CommonModule as CommonModule;
31use oat\tao\model\http\formatter\ResponseFormatter;
32use oat\tao\model\http\response\ErrorJsonResponse;
33use oat\tao\model\http\response\SuccessJsonResponse;
34use oat\taoMediaManager\model\sharedStimulus\css\service\LoadStylesheetService;
35use oat\taoMediaManager\model\sharedStimulus\css\handler\LoadStylesheetHandler;
36use oat\taoMediaManager\model\sharedStimulus\css\service\ListStylesheetsService;
37use oat\taoMediaManager\model\sharedStimulus\css\handler\ListStylesheetsHandler;
38use oat\taoMediaManager\model\sharedStimulus\css\service\LoadStylesheetClassesService;
39use oat\taoMediaManager\model\sharedStimulus\css\service\SaveStylesheetClassesService;
40use oat\taoMediaManager\model\sharedStimulus\css\handler\SaveStylesheetClassesHandler;
41use oat\taoMediaManager\model\sharedStimulus\css\handler\LoadStylesheetClassesHandler;
42
43class SharedStimulusStyling extends CommonModule
44{
45    use LoggerAwareTrait;
46
47    public function save(
48        ResponseFormatter $responseFormatter,
49        SaveStylesheetClassesHandler $saveStylesheetClassesHandler,
50        SaveStylesheetClassesService $saveStylesheetClassesService
51    ): void {
52        $formatter = $responseFormatter->withJsonHeader();
53
54        try {
55            $saveStylesheetClassesDTO = $saveStylesheetClassesHandler($this->getPsrRequest());
56            $saveStylesheetClassesService->save($saveStylesheetClassesDTO);
57
58            $formatter->withBody(new SuccessJsonResponse([]));
59        } catch (Throwable $exception) {
60            $this->logError(sprintf('Error saving passage styles: %s', $exception->getMessage()));
61
62            $formatter
63                ->withStatusCode(400)
64                ->withBody(new ErrorJsonResponse($exception->getCode(), $exception->getMessage()));
65        }
66
67        $this->setResponse($formatter->format($this->getPsrResponse()));
68    }
69
70    public function load(
71        ResponseFormatter $responseFormatter,
72        LoadStylesheetClassesHandler $loadStylesheetClassesHandler,
73        LoadStylesheetClassesService $loadStylesheetClassesService
74    ): void {
75        $formatter = $responseFormatter->withJsonHeader();
76
77        try {
78            $loadStylesheetClassesDTO = $loadStylesheetClassesHandler($this->getPsrRequest());
79            $data = $loadStylesheetClassesService->load($loadStylesheetClassesDTO);
80
81            $formatter->withBody(new SuccessJsonResponse($data));
82        } catch (Throwable $exception) {
83            $this->logError(sprintf('Error loading passage styles: %s', $exception->getMessage()));
84
85            $formatter
86                ->withStatusCode(400)
87                ->withBody(new ErrorJsonResponse($exception->getCode(), $exception->getMessage()));
88        }
89
90        $this->setResponse($formatter->format($this->getPsrResponse()));
91    }
92
93    public function getStylesheets(
94        ResponseFormatter $responseFormatter,
95        ListStylesheetsHandler $listStylesheetsHandler,
96        ListStylesheetsService $listStylesheetsService
97    ): void {
98        $formatter = $responseFormatter->withJsonHeader();
99
100        try {
101            $listStylesheetsDTO = $listStylesheetsHandler($this->getPsrRequest());
102            $data = $listStylesheetsService->getList($listStylesheetsDTO);
103
104            $formatter->withBody(new SuccessJsonResponse($data));
105        } catch (Throwable $exception) {
106            $this->logError(sprintf('Error loading passage stylesheets: %s', $exception->getMessage()));
107
108            $formatter
109                ->withStatusCode(400)
110                ->withBody(new ErrorJsonResponse($exception->getCode(), $exception->getMessage()));
111        }
112
113        $this->setResponse($formatter->format($this->getPsrResponse()));
114    }
115
116    public function loadStylesheet(
117        ResponseFormatter $responseFormatter,
118        LoadStylesheetHandler $loadStylesheetHandler,
119        LoadStylesheetService $loadStylesheetService
120    ): void {
121        try {
122            $loadStylesheetDTO = $loadStylesheetHandler($this->getPsrRequest());
123            $stream = $loadStylesheetService->load($loadStylesheetDTO);
124
125            HttpHelper::returnStream($stream, 'text/css');
126        } catch (Throwable $exception) {
127            $this->logError(sprintf('Error loading passage stylesheet: %s', $exception->getMessage()));
128
129            $formatter = $responseFormatter->withJsonHeader();
130            $formatter
131                ->withStatusCode(400)
132                ->withBody(new ErrorJsonResponse($exception->getCode(), $exception->getMessage()));
133
134            $this->setResponse($formatter->format($this->getPsrResponse()));
135        }
136    }
137
138    public function upload(
139        UploadStylesheetRequestHandler $uploadStylesheetHandler,
140        UploadStylesheetService $uploadStylesheetService,
141        ResponseFormatter $responseFormatter
142    ): void {
143        $formatter = $responseFormatter->withJsonHeader();
144
145        try {
146            $uploadedStylesheet = $uploadStylesheetHandler($this->getPsrRequest());
147            $data = $uploadStylesheetService->save($uploadedStylesheet);
148
149            $formatter->withBody($data);
150        } catch (Throwable $exception) {
151            $this->logError(sprintf('Error uploading passage stylesheets: %s', $exception->getMessage()));
152
153            $formatter
154                ->withStatusCode(400)
155                ->withBody(new ErrorJsonResponse($exception->getCode(), $exception->getMessage()));
156        }
157
158        $this->setResponse($formatter->format($this->getPsrResponse()));
159    }
160}