Completed
Push — master ( 9c5fdf...27a5d2 )
by John
7s
created

VndErrorResponseFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Response\ErrorResponseFactory;
10
11
use KleijnWeb\SwaggerBundle\Exception\InvalidParametersException;
12
use KleijnWeb\SwaggerBundle\Response\Error\HttpError;
13
use KleijnWeb\SwaggerBundle\Response\ErrorResponseFactory;
14
use KleijnWeb\SwaggerBundle\Response\ErrorResponseFactory\VndError\VndErrorResponse;
15
use KleijnWeb\SwaggerBundle\Response\ErrorResponseFactory\VndError\VndValidationErrorFactory;
16
use Ramsey\VndError\VndError;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * @author John Kleijn <[email protected]>
21
 */
22
class VndErrorResponseFactory implements ErrorResponseFactory
23
{
24
    /**
25
     * @var VndValidationErrorFactory;
26
     */
27
    private $vndValidationErrorFactory;
28
29
    /**
30
     * VndValidationErrorFactory constructor.
31
     *
32
     * @param VndValidationErrorFactory $vndValidationErrorFactory
33
     */
34
    public function __construct(VndValidationErrorFactory $vndValidationErrorFactory)
35
    {
36
        $this->vndValidationErrorFactory = $vndValidationErrorFactory;
37
    }
38
39
    /**
40
     * @param HttpError $error
41
     *
42
     * @return Response
43
     */
44
    public function create(HttpError $error): Response
45
    {
46
        $exception = $error->getException();
47
        if (!$exception instanceof InvalidParametersException) {
48
            return $this->createResponseWithError($error, new VndError($error->getMessage(), $error->getLogRef()));
49
        }
50
51
        return $this->createResponseWithError(
52
            $error,
53
            $this->vndValidationErrorFactory->create(
54
                $error->getRequest(),
55
                $exception,
56
                $error->getLogRef()
57
            )
58
        );
59
    }
60
61
    /**
62
     * @param HttpError $httpError
63
     * @param VndError  $vndError
64
     *
65
     * @return VndErrorResponse
66
     */
67
    private function createResponseWithError(HttpError $httpError, VndError $vndError): VndErrorResponse
68
    {
69
        return new VndErrorResponse($vndError, $httpError->getStatusCode());
70
    }
71
}
72