Issues (16)

docs/validation/XmlRequestBodyValidator.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Example;
6
7
use Nijens\OpenapiBundle\ExceptionHandling\Exception\InvalidRequestBodyProblemException;
8
use Nijens\OpenapiBundle\ExceptionHandling\Exception\ProblemException;
9
use Nijens\OpenapiBundle\ExceptionHandling\Exception\RequestProblemExceptionInterface;
10
use Nijens\OpenapiBundle\Validation\RequestValidator\ValidatorInterface;
11
use Symfony\Component\HttpFoundation\HeaderUtils;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class XmlRequestBodyValidator implements ValidatorInterface
16
{
17
    public function validate(Request $request): ?RequestProblemExceptionInterface {
18
        $requestContentType = $this->getRequestContentType($request);
19
        if ($requestContentType !== 'application/xml') {
20
            return null;
21
        }
22
23
        $requestBody = $request->getContent();
24
        $violations = $this->validateAgainstSchema($requestBody);
0 ignored issues
show
The method validateAgainstSchema() does not exist on App\Example\XmlRequestBodyValidator. Did you maybe mean validate()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        $violations = $this->validateAgainstSchema($requestBody);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
        if (count($violations) > 0) {
26
            $exception = new InvalidRequestBodyProblemException(
27
                ProblemException::DEFAULT_TYPE_URI,
28
                'The request body contains errors.',
29
                Response::HTTP_BAD_REQUEST,
30
                'Validation of XML request body failed.'
31
            );
32
33
            return $exception->withViolations($violations);
34
        }
35
36
        return null;
37
    }
38
39
    private function getRequestContentType(Request $request): string
40
    {
41
        return current(HeaderUtils::split($request->headers->get('Content-Type', ''), ';')) ?: '';
42
    }
43
44
    // ...
45
}
46