Passed
Push — main ( 936852...10149e )
by Thierry
15:09
created

Handler::getCallableObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Exceptions;
4
5
use App\Ajax\Web\Planning\Pool;
6
use App\Ajax\Web\Planning\Round;
7
use App\Ajax\Web\Tontine\Member;
8
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9
use Jaxon\Laravel\Jaxon;
10
use Jaxon\Plugin\Request\CallableClass\CallableRegistry;
11
use Siak\Tontine\Exception\AuthenticationException;
12
use Siak\Tontine\Exception\MessageException;
13
use Siak\Tontine\Exception\PlanningPoolException;
14
use Siak\Tontine\Exception\PlanningRoundException;
15
use Siak\Tontine\Exception\TontineMemberException;
16
use Siak\Tontine\Service\TenantService;
17
use Throwable;
18
19
use function Jaxon\jaxon;
20
use function app;
21
use function route;
22
use function trans;
23
24
class Handler extends ExceptionHandler
25
{
26
    /**
27
     * A list of exception types with their corresponding custom log levels.
28
     *
29
     * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Thro...e>, \Psr\Log\LogLevel:: at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Throwable>, \Psr\Log\LogLevel::.
Loading history...
30
     */
31
    protected $levels = [
32
        //
33
    ];
34
35
    /**
36
     * A list of the exception types that are not reported.
37
     *
38
     * @var array<int, class-string<\Throwable>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, class-string<\Throwable>> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<int, class-string<\Throwable>>.
Loading history...
39
     */
40
    protected $dontReport = [
41
        AuthenticationException::class,
42
        MessageException::class,
43
        PlanningPoolException::class,
44
        PlanningRoundException::class,
45
        TontineMemberException::class,
46
    ];
47
48
    /**
49
     * A list of the inputs that are never flashed to the session on validation exceptions.
50
     *
51
     * @var array<int, string>
52
     */
53
    protected $dontFlash = [
54
        'current_password',
55
        'password',
56
        'password_confirmation',
57
    ];
58
59
    /**
60
     * Get the instance of a callable class registered with Jaxon
61
     *
62
     * @param string $class
63
     *
64
     * @return mixed
65
     */
66
    private function getCallableObject(string $class)
67
    {
68
        // Todo: implement this feature in the Jaxon library.
69
        $registry = jaxon()->di()->g(CallableRegistry::class);
70
        $callable = $registry->getCallableObject($class)->getRegisteredObject();
71
72
        $tenantService = app()->make(TenantService::class);
73
        return $callable->setTenantService($tenantService);
74
    }
75
76
    /**
77
     * Register the exception handling callbacks for the application.
78
     *
79
     * @return void
80
     */
81
    public function register()
82
    {
83
        $this->reportable(function (Throwable $e) {
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed. ( Ignorable by Annotation )

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

83
        $this->reportable(function (/** @scrutinizer ignore-unused */ Throwable $e) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
            //
85
        });
86
87
        // Redirect to the login page
88
        $this->renderable(function (AuthenticationException $e) {
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed. ( Ignorable by Annotation )

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

88
        $this->renderable(function (/** @scrutinizer ignore-unused */ AuthenticationException $e) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
            $jaxon = app()->make(Jaxon::class);
90
            $ajaxResponse = $jaxon->ajaxResponse();
91
            $ajaxResponse->redirect(route('login'));
92
93
            return $jaxon->httpResponse();
94
        });
95
96
        // Show the error message in a dialog
97
        $this->renderable(function (MessageException $e) {
98
            $jaxon = app()->make(Jaxon::class);
99
            $ajaxResponse = $jaxon->ajaxResponse();
100
            $ajaxResponse->dialog->error($e->getMessage(), trans('common.titles.error'));
0 ignored issues
show
Bug introduced by
Accessing dialog on the interface Jaxon\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
101
102
            return $jaxon->httpResponse();
103
        });
104
105
        // Show the warning message in a dialog
106
        $this->renderable(function (PlanningRoundException $e) {
107
            $this->getCallableObject(Round::class)->home();
108
109
            $jaxon = app()->make(Jaxon::class);
110
            $ajaxResponse = $jaxon->ajaxResponse();
111
            $ajaxResponse->dialog->warning($e->getMessage(), trans('common.titles.warning'));
0 ignored issues
show
Bug introduced by
Accessing dialog on the interface Jaxon\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
112
113
            return $jaxon->httpResponse();
114
        });
115
116
        // Show the warning message in a dialog
117
        $this->renderable(function (PlanningPoolException $e) {
118
            $this->getCallableObject(Pool::class)->home();
119
120
            $jaxon = app()->make(Jaxon::class);
121
            $ajaxResponse = $jaxon->ajaxResponse();
122
            $ajaxResponse->dialog->warning($e->getMessage(), trans('common.titles.warning'));
0 ignored issues
show
Bug introduced by
Accessing dialog on the interface Jaxon\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
123
124
            return $jaxon->httpResponse();
125
        });
126
127
        // Show the warning message in a dialog
128
        $this->renderable(function (TontineMemberException $e) {
129
            $this->getCallableObject(Member::class)->home();
130
131
            $jaxon = app()->make(Jaxon::class);
132
            $ajaxResponse = $jaxon->ajaxResponse();
133
            $ajaxResponse->dialog->warning($e->getMessage(), trans('common.titles.warning'));
0 ignored issues
show
Bug introduced by
Accessing dialog on the interface Jaxon\Response\ResponseInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
134
135
            return $jaxon->httpResponse();
136
        });
137
    }
138
}
139