RollbarListener::onKernelResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Ftrrtf\RollbarBundle\EventListener;
4
5
use Ftrrtf\Rollbar\ErrorHandler;
6
use Ftrrtf\Rollbar\Notifier;
7
use Ftrrtf\RollbarBundle\Helper\UserHelper;
8
use Symfony\Component\Console\Event\ConsoleCommandEvent;
9
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
10
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
11
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
12
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
13
use Symfony\Component\HttpKernel\Exception\HttpException;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16
17
/**
18
 * Rollbar framework Listener.
19
 */
20
class RollbarListener
21
{
22
    /**
23
     * @var Notifier
24
     */
25
    protected $notifier;
26
27
    /**
28
     * @var TokenStorageInterface
29
     */
30
    protected $tokenStorage;
31
32
    /**
33
     * @var AuthorizationCheckerInterface
34
     */
35
    protected $authorizationChecker;
36
37
    /**
38
     * @var \Exception
39
     */
40
    protected $exception;
41
42
    /**
43
     * @var ErrorHandler
44
     */
45
    private $errorHandler;
46
    /**
47
     * @var UserHelper
48
     */
49
    private $userHelper;
50
51
    /**
52
     * Init.
53
     *
54
     * @param Notifier                      $notifier
55
     * @param ErrorHandler                  $errorHandler
56
     * @param TokenStorageInterface         $tokenStorage
57
     * @param AuthorizationCheckerInterface $authorizationChecker
58
     * @param UserHelper                    $userHelper
59
     */
60
    public function __construct(
61
        Notifier $notifier,
62
        ErrorHandler $errorHandler,
63
        TokenStorageInterface $tokenStorage,
64
        AuthorizationCheckerInterface $authorizationChecker,
65
        UserHelper $userHelper
66
    ) {
67
        $this->notifier = $notifier;
68
        $this->errorHandler = $errorHandler;
69
        $this->tokenStorage = $tokenStorage;
70
        $this->authorizationChecker = $authorizationChecker;
71
        $this->userHelper = $userHelper;
72
73
        $self = $this;
74
        $this->notifier->getEnvironment()
75
            ->setOption(
76
                'person_callback',
77
                function () use ($self) {
78
                    return $self->getUserData();
79
                }
80
            );
81
    }
82
83
    /**
84
     * Register error handler.
85
     *
86
     * @param GetResponseEvent $event
87
     */
88
    public function onKernelRequest(GetResponseEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

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

Loading history...
89
    {
90
        $this->errorHandler->registerErrorHandler($this->notifier);
91
        $this->errorHandler->registerShutdownHandler($this->notifier);
92
    }
93
94
    /**
95
     * Save exception.
96
     *
97
     * @param GetResponseForExceptionEvent $event
98
     */
99
    public function onKernelException(GetResponseForExceptionEvent $event)
100
    {
101
        // Skip HTTP exception
102
        if ($event->getException() instanceof HttpException) {
103
            return;
104
        }
105
106
        $this->setException($event->getException());
107
    }
108
109
    /**
110
     * @param ConsoleCommandEvent $event
111
     */
112
    public function onConsoleCommand(ConsoleCommandEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

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

Loading history...
113
    {
114
        $this->errorHandler->registerExceptionHandler($this->notifier);
115
    }
116
117
    /**
118
     * @param ConsoleExceptionEvent $event
119
     */
120
    public function onConsoleException(ConsoleExceptionEvent $event)
121
    {
122
        $this->notifier->reportException($event->getException());
123
    }
124
125
    /**
126
     * Wrap exception with additional info.
127
     *
128
     * @param FilterResponseEvent $event
129
     */
130
    public function onKernelResponse(FilterResponseEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

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

Loading history...
131
    {
132
        if ($this->getException()) {
133
            $this->notifier->reportException($this->getException());
134
            $this->setException(null);
135
        }
136
    }
137
138
    /**
139
     * Get current user info.
140
     *
141
     * @return null|array
142
     */
143
    public function getUserData()
144
    {
145
        if (!$this->tokenStorage->getToken()
146
            || !$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')
147
        ) {
148
            return null;
149
        }
150
151
        $user = $this->tokenStorage->getToken()->getUser();
152
153
        if (!$user) {
154
            return null;
155
        }
156
157
        return $this->userHelper->buildUserData($user);
158
    }
159
160
    /**
161
     * @return \Exception
162
     */
163
    public function getException()
164
    {
165
        return $this->exception;
166
    }
167
168
    /**
169
     * @param \Exception|null $exception
170
     */
171
    public function setException($exception)
172
    {
173
        $this->exception = $exception;
174
    }
175
}
176