Completed
Push — master ( 11ab51...fc8810 )
by Alexander
04:57
created

HandlesApiErrors::transformValidationException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Traits;
4
5
use Exception;
6
use Flugg\Responder\Exceptions\Http\ApiException;
7
use Flugg\Responder\Exceptions\Http\ResourceNotFoundException;
8
use Flugg\Responder\Exceptions\Http\UnauthenticatedException;
9
use Flugg\Responder\Exceptions\Http\UnauthorizedException;
10
use Flugg\Responder\Exceptions\Http\ValidationFailedException;
11
use Illuminate\Auth\Access\AuthorizationException;
12
use Illuminate\Auth\AuthenticationException;
13
use Illuminate\Database\Eloquent\ModelNotFoundException;
14
use Illuminate\Database\Eloquent\RelationNotFoundException;
15
use Illuminate\Http\JsonResponse;
16
use Illuminate\Validation\ValidationException;
17
18
/**
19
 * Use this trait in your exceptions handler to give you access to methods you may
20
 * use to let the package catch and handle any API exceptions.
21
 *
22
 * @package flugger/laravel-responder
23
 * @author  Alexander Tømmerås <[email protected]>
24
 * @license The MIT License
25
 */
26
trait HandlesApiErrors
27
{
28
    /**
29
     * Transform a Laravel exception into an API exception.
30
     *
31
     * @param  Exception $exception
32
     * @return void
33
     */
34
    protected function transformException(Exception $exception)
35
    {
36
        $this->transformAuthException($exception);
37
        $this->transformEloquentException($exception);
38
        $this->transformValidationException($exception);
39
    }
40
41
    /**
42
     * Transform a Laravel auth exception into an API exception.
43
     *
44
     * @param  Exception $exception
45
     * @return void
46
     * @throws UnauthenticatedException
47
     * @throws UnauthorizedException
48
     */
49
    protected function transformAuthException(Exception $exception)
50
    {
51
        if ($exception instanceof AuthenticationException) {
52
            throw new UnauthenticatedException();
53
        }
54
55
        if ($exception instanceof AuthorizationException) {
56
            throw new UnauthorizedException();
57
        }
58
    }
59
60
    /**
61
     * Transform an Eloquent exception into an API exception.
62
     *
63
     * @param  Exception $exception
64
     * @return void
65
     * @throws ResourceNotFoundException
66
     * @throws RelationNotFoundException
67
     */
68
    protected function transformEloquentException(Exception $exception)
69
    {
70
        if ($exception instanceof ModelNotFoundException) {
71
            throw new ResourceNotFoundException();
72
        }
73
74
        if ($exception instanceof RelationNotFoundException) {
75
            throw new RelationNotFoundException();
76
        }
77
    }
78
79
    /**
80
     * Transform a Laravel validation exception into an API exception.
81
     *
82
     * @param  Exception $exception
83
     * @return void
84
     * @throws ValidationFailedException
85
     */
86
    protected function transformValidationException(Exception $exception)
87
    {
88
        if ($exception instanceof ValidationException) {
89
            throw new ValidationFailedException($exception->validator);
90
        }
91
    }
92
93
    /**
94
     * Renders any API exception into a JSON error response.
95
     *
96
     * @param  ApiException $exception
97
     * @return JsonResponse
98
     */
99
    protected function renderApiError(ApiException $exception):JsonResponse
100
    {
101
        return app('responder.error')
102
            ->setError($exception->getErrorCode(), $exception->getMessage())
103
            ->addData($exception->getData())
104
            ->respond($exception->getStatusCode());
105
    }
106
}