Passed
Pull Request — master (#144)
by Jérémy
02:16
created

DeprecationListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register() 0 13 4
A unregister() 0 7 2
1
<?php
2
3
/*
4
 * This file is part of Ekino New Relic bundle.
5
 *
6
 * (c) Ekino - Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ekino\Bundle\NewRelicBundle\Listener;
13
14
use Ekino\Bundle\NewRelicBundle\Exception\DeprecationException;
15
use Ekino\Bundle\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
16
17
class DeprecationListener
18
{
19
    private $isRegistered = false;
20
    private $interactor;
21
22
    public function __construct(NewRelicInteractorInterface $interactor)
23
    {
24
        $this->interactor = $interactor;
25
    }
26
27
    public function register(): void
28
    {
29
        if ($this->isRegistered) {
30
            return;
31
        }
32
        $this->isRegistered = true;
33
34
        $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$prevErrorHandler) {
0 ignored issues
show
Unused Code introduced by
The assignment to $prevErrorHandler is dead and can be removed.
Loading history...
35
            if (E_USER_DEPRECATED === $type) {
36
                $this->interactor->noticeThrowable(new DeprecationException($msg, 0, $type, $file, $line));
37
            }
38
39
            return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
40
        });
41
    }
42
43
    public function unregister(): void
44
    {
45
        if (!$this->isRegistered) {
46
            return;
47
        }
48
        $this->isRegistered = false;
49
        restore_error_handler();
50
    }
51
}
52