Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DevelopController
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 pdf
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 isDebugMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Controller;
10
11use OCA\Libresign\AppInfo\Application;
12use OCP\AppFramework\Controller;
13use OCP\AppFramework\Http;
14use OCP\AppFramework\Http\Attribute\FrontpageRoute;
15use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
16use OCP\AppFramework\Http\Attribute\OpenAPI;
17use OCP\AppFramework\Http\Attribute\PublicPage;
18use OCP\AppFramework\Http\DataResponse;
19use OCP\AppFramework\Http\FileDisplayResponse;
20use OCP\AppFramework\Http\Response;
21use OCP\Files\SimpleFS\InMemoryFile;
22use OCP\IConfig;
23use OCP\IRequest;
24
25#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
26class DevelopController extends Controller {
27    public function __construct(
28        IRequest $request,
29        private IConfig $config,
30    ) {
31        parent::__construct(Application::APP_ID, $request);
32    }
33
34    /**
35     * Get a demo PDF file to be used by test purpose
36     *
37     * To use this endpoint is necessary to enable the debug mode in your instance. To do this, run the command:
38     *
39     * `occ config:system:set debug --value true --type boolean`
40     *
41     * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: 'application/pdf',Content-Disposition: 'inline; filename="file.pdf"'}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
42     *
43     * 200: PDF returned
44     * 404: Debug mode not enabled
45     * @psalm-suppress InvalidReturnType
46     */
47    #[NoCSRFRequired]
48    #[PublicPage]
49    #[FrontpageRoute(verb: 'GET', url: '/develop/pdf')]
50    public function pdf(): FileDisplayResponse|Response {
51        if (!$this->isDebugMode()) {
52            return new DataResponse([], Http::STATUS_NOT_FOUND);
53        }
54        $file = new InMemoryFile('file.pdf', file_get_contents(__DIR__ . '/../../tests/php/fixtures/pdfs/small_valid.pdf'));
55        $response = new FileDisplayResponse($file);
56        $response->setHeaders([
57            'Content-Disposition' => 'inline; filename="file.pdf"',
58            'Content-Type' => 'application/pdf',
59        ]);
60        /** @psalm-suppress InvalidReturnStatement */
61        return $response;
62    }
63
64    public function isDebugMode(): bool {
65        return $this->config->getSystemValue('debug', false) === true;
66    }
67}