ExceptionListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 46
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 4
A onKernelException() 0 26 3
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\EventListener;
4
5
use PiouPiou\RibsAdminBundle\Entity\User;
6
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
7
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
8
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
9
10
class ExceptionListener
11
{
12
    /**
13
     * @var ParameterBagInterface
14
     */
15
    private $paramter;
16
17
    /**
18
     * @var User
19
     */
20
    private $user;
21
22
    public function __construct(ParameterBagInterface $parameterBag, TokenStorageInterface $tokenStorage)
23
    {
24
        $this->paramter = $parameterBag;
25
        if ($tokenStorage->getToken() && is_object($tokenStorage->getToken()->getUser()) && $tokenStorage->getToken()->getUser()->getUser()) {
0 ignored issues
show
Bug introduced by
The method getUser() does not exist on Stringable. ( Ignorable by Annotation )

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

25
        if ($tokenStorage->getToken() && is_object($tokenStorage->getToken()->getUser()) && $tokenStorage->getToken()->getUser()->/** @scrutinizer ignore-call */ getUser()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getUser() does not exist on Symfony\Component\Security\Core\User\UserInterface. Did you maybe mean getUsername()? ( Ignorable by Annotation )

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

25
        if ($tokenStorage->getToken() && is_object($tokenStorage->getToken()->getUser()) && $tokenStorage->getToken()->getUser()->/** @scrutinizer ignore-call */ getUser()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
            $this->user = $tokenStorage->getToken()->getUser()->getUser();
27
        }
28
    }
29
30
    public function onKernelException(ExceptionEvent $event)
31
    {
32
        $slack_webhook = $this->paramter->get('ribs_admin.slack_webhook');
33
        if ($slack_webhook) {
34
            $data = array();
35
            $data['channel'] = '#errors';
36
            $data['username'] = $_SERVER['HTTP_HOST'];
37
            $data['text'] = "• *Erreur* : " . strip_tags($event->getThrowable()->getMessage());
38
            $data['text'] .= "\n• *Erreur File* : " . strip_tags($event->getThrowable()->getFile()) . " at line :" . strip_tags($event->getThrowable()->getLine());
39
            $data['text'] .= "\n• *URL* : " . $event->getRequest()->getUri();
40
41
            if ($this->user) {
42
                $data['text'] .= "\n• *Utilisateur* : " . $this->user . " with id : " . $this->user->getId();
43
            }
44
45
            $data['unfurl_links'] = false;
46
            $data_json = json_encode($data);
47
48
            $ch = curl_init();
49
            curl_setopt($ch, CURLOPT_URL, $slack_webhook);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

49
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $slack_webhook);
Loading history...
50
            curl_setopt($ch, CURLOPT_POST, 1);
51
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
52
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
53
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
54
            curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

54
            curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
55
            curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

55
            curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
56
        }
57
    }
58
}