ErrorBuilder   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 40.26%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 7
dl 0
loc 114
ccs 31
cts 77
cp 0.4026
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configureError() 0 7 1
A createErrorFromException() 0 4 1
D buildErrorFromException() 0 76 19
A fillErrorFields() 0 14 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Service;
5
6
use Paysera\Component\ObjectWrapper\Exception\InvalidItemException;
7
use Paysera\Component\Normalization\Exception\InvalidDataException;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
12
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
13
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
14
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
15
use Maba\Bundle\RestBundle\Entity\Error;
16
use Maba\Bundle\RestBundle\Exception\ApiException;
17
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
18
use Symfony\Component\Security\Core\Exception\AuthenticationException;
19
use Exception;
20
21
class ErrorBuilder implements ErrorBuilderInterface
22
{
23
    private $errorConfiguration;
24
25 81
    public function __construct()
26
    {
27 81
        $this->errorConfiguration = [];
28 81
    }
29
30 81
    public function configureError(string $errorCode, int $statusCode, string $message)
31
    {
32 81
        $this->errorConfiguration[$errorCode] = [
33 81
            'statusCode' => $statusCode,
34 81
            'message' => $message,
35
        ];
36 81
    }
37
38 33
    public function createErrorFromException(Exception $exception): Error
39
    {
40 33
        return $this->fillErrorFields($this->buildErrorFromException($exception));
41
    }
42
43 33
    private function buildErrorFromException(Exception $exception): Error
44
    {
45 33
        if ($exception instanceof ApiException) {
46 30
            return (new Error())
47 30
                ->setCode($exception->getErrorCode())
48 30
                ->setMessage($exception->getMessage())
49 30
                ->setStatusCode($exception->getStatusCode())
50 30
                ->setProperties($exception->getProperties())
51 30
                ->setData($exception->getData())
52 30
                ->setViolations($exception->getViolations())
53
            ;
54 3
        } elseif ($exception instanceof InvalidDataException) {
55
            return (new Error())
56
                ->setCode(ApiException::INVALID_PARAMETERS)
57
                ->setMessage($exception->getMessage())
58
            ;
59 3
        } elseif ($exception instanceof InvalidItemException) {
60 3
            return (new Error())
61 3
                ->setCode(ApiException::INVALID_PARAMETERS)
62 3
                ->setMessage($exception->getMessage())
63
            ;
64
        } elseif ($exception instanceof AuthenticationCredentialsNotFoundException) {
65
            return (new Error())
66
                ->setCode(ApiException::UNAUTHORIZED)
67
                ->setMessage('No authorization data found')
68
            ;
69
        } elseif ($exception instanceof AuthenticationException) {
70
            $error = (new Error())->setCode(ApiException::UNAUTHORIZED);
71
            if ($exception->getCode() === 999) {
72
                $error->setMessage($exception->getMessage());
73
            }
74
            return $error;
75
        } elseif ($exception instanceof AccessDeniedException) {
76
            return (new Error())
77
                ->setCode(ApiException::FORBIDDEN)
78
                ->setMessage($exception->getMessage())
79
            ;
80
        } elseif ($exception instanceof AccessDeniedHttpException) {
81
            return (new Error())
82
                ->setCode(ApiException::FORBIDDEN)
83
                ->setMessage($exception->getMessage())
84
            ;
85
        } elseif ($exception instanceof ResourceNotFoundException || $exception instanceof NotFoundHttpException) {
86
            return (new Error())
87
                ->setCode(ApiException::NOT_FOUND)
88
                ->setMessage('Provided url not found')
89
            ;
90
        } elseif ($exception instanceof MethodNotAllowedException) {
91
            return (new Error())
92
                ->setCode(ApiException::NOT_FOUND)
93
                ->setMessage('Provided method not allowed for this url')
94
                ->setStatusCode(Response::HTTP_METHOD_NOT_ALLOWED)
95
            ;
96
        } elseif ($exception instanceof HttpExceptionInterface && $exception->getStatusCode() < 500) {
97
            if ($exception->getStatusCode() === Response::HTTP_NOT_FOUND) {
98
                return (new Error())
99
                    ->setCode(ApiException::NOT_FOUND)
100
                    ->setStatusCode($exception->getStatusCode())
101
                ;
102
            } elseif ($exception->getStatusCode() === Response::HTTP_METHOD_NOT_ALLOWED) {
103
                return (new Error())
104
                    ->setCode(ApiException::NOT_FOUND)
105
                    ->setStatusCode($exception->getStatusCode())
106
                    ->setMessage('Provided method not allowed for this url')
107
                ;
108
            } elseif ($exception->getStatusCode() === 401) {
109
                return (new Error())->setCode(ApiException::UNAUTHORIZED);
110
            } elseif ($exception->getStatusCode() === 403) {
111
                return (new Error())->setCode(ApiException::FORBIDDEN);
112
            } elseif ($exception->getStatusCode() === 400) {
113
                return (new Error())->setCode(ApiException::INVALID_REQUEST);
114
            }
115
        }
116
117
        return (new Error())->setCode(ApiException::INTERNAL_SERVER_ERROR)->setStatusCode(500);
118
    }
119
120 33
    private function fillErrorFields(Error $error): Error
121
    {
122 33
        $configuration = $this->errorConfiguration[$error->getCode()] ?? [];
123
124 33
        if ($error->getStatusCode() === null) {
125 33
            $error->setStatusCode($configuration['statusCode'] ?? Response::HTTP_BAD_REQUEST);
126
        }
127
128 33
        if ($error->getMessage() === null || $error->getMessage() === '') {
129 12
            $error->setMessage($configuration['message'] ?? null);
130
        }
131
132 33
        return $error;
133
    }
134
}
135