Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
8 / 14
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CaptchaVerifyController
57.14% covered (warning)
57.14%
8 / 14
50.00% covered (danger)
50.00%
1 / 2
6.97
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 readResponse
53.85% covered (warning)
53.85%
7 / 13
0.00% covered (danger)
0.00%
0 / 1
5.57
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Controllers\Captcha;
6
7use BO\Zmscitizenapi\BaseController;
8use BO\Zmscitizenapi\Utils\ErrorMessages;
9use BO\Zmscitizenapi\Services\Captcha\CaptchaService;
10use BO\Zmscitizenapi\Services\Core\ValidationService;
11use Psr\Http\Message\RequestInterface;
12use Psr\Http\Message\ResponseInterface;
13
14class CaptchaVerifyController extends BaseController
15{
16    private CaptchaService $service;
17    /** @psalm-api */
18    public function __construct()
19    {
20        $this->service = new CaptchaService();
21    }
22
23    #[\Override]
24    public function readResponse(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
25    {
26        $requestErrors = ValidationService::validateServerPostRequest($request);
27
28        if (!empty($requestErrors['errors'])) {
29            return $this->createJsonResponse(
30                $response,
31                $requestErrors,
32                ErrorMessages::get('invalidRequest')['statusCode']
33            );
34        }
35
36        $data = $request->getParsedBody();
37        $payload = $data['payload'] ?? null;
38        $result = $this->service->verifySolution($payload);
39        return is_array($result) && isset($result['errors'])
40            ? $this->createJsonResponse($response, $result, ErrorMessages::getHighestStatusCode($result['errors']))
41            : $this->createJsonResponse($response, $result, 200);
42    }
43}