Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CaptchaController
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 renderImage
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * The Captcha Image Controller
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2024-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2024-03-12
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Frontend\Api;
21
22use phpMyFAQ\Captcha\BuiltinCaptcha;
23use phpMyFAQ\Captcha\CaptchaInterface;
24use phpMyFAQ\Controller\AbstractController;
25use Symfony\Component\HttpFoundation\Response;
26use Symfony\Component\Routing\Attribute\Route;
27
28final class CaptchaController extends AbstractController
29{
30    public function __construct(
31        private readonly CaptchaInterface $captcha,
32    ) {
33        parent::__construct();
34    }
35
36    /**
37     * @throws \JsonException|\Exception
38     */
39    #[Route(path: 'captcha', name: 'api.private.captcha', methods: ['GET'])]
40    public function renderImage(): Response
41    {
42        if (!$this->captcha instanceof BuiltinCaptcha) {
43            return new Response('', Response::HTTP_NOT_FOUND);
44        }
45
46        $response = new Response();
47        $response->setStatusCode(Response::HTTP_OK);
48        $response->headers->set('Content-Type', 'image/jpeg');
49
50        // Set image content
51        $response->setContent($this->captcha->getCaptchaImage());
52
53        return $response;
54    }
55}