ExceptionHandler::unregister()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Error;
26
27
use Brickoo\Component\Error\Messaging\Message\ExceptionMessage;
28
use Brickoo\Component\Error\Exception\DuplicateHandlerRegistrationException;
29
use Brickoo\Component\Error\Exception\HandlerNotRegisteredException;
30
use Brickoo\Component\Messaging\MessageDispatcher;
31
32
/**
33
 * ExceptionHandler
34
 *
35
 * Handles user defined or system exception.
36
 * Exceptions can be logged through the log message which is triggered if exceptions occurred.
37
 * @author Celestino Diaz <[email protected]>
38
 */
39
class ExceptionHandler {
40
41
    /** @var boolean */
42
    private $isRegistered;
43
44
    /** @var \Brickoo\Component\Messaging\MessageDispatcher */
45
    private $messageDispatcher;
46
47
    /**
48
     * Class constructor.
49
     * @param \Brickoo\Component\Messaging\MessageDispatcher $messageDispatcher
50
     */
51 1
    public function __construct(MessageDispatcher $messageDispatcher) {
52 1
        $this->messageDispatcher = $messageDispatcher;
53 1
        $this->isRegistered = false;
54 1
    }
55
56
    /**
57
     * Checks if the instance is registered as an exception handler.
58
     * @return boolean check result
59
     */
60 1
    public function isRegistered() {
61 1
        return ($this->isRegistered === true);
62
    }
63
64
    /**
65
     * Registers the instance as exception handler.
66
     * @throws \Brickoo\Component\Error\Exception\DuplicateHandlerRegistrationException
67
     * @return \Brickoo\Component\Error\ExceptionHandler
68
     */
69 2
    public function register() {
70 2
        if ($this->isRegistered()) {
71 1
            throw new DuplicateHandlerRegistrationException("ExceptionHandler");
72
        }
73 2
        set_exception_handler([$this, "handleException"]);
74 2
        $this->isRegistered = true;
75 2
        return $this;
76
    }
77
78
    /**
79
     * Unregister the instance as exception handler by restoring previous exception handler.
80
     * @throws \Brickoo\Component\Error\Exception\HandlerNotRegisteredException
81
     * @return \Brickoo\Component\Error\ExceptionHandler
82
     */
83 2
    public function unregister() {
84 2
        if (!$this->isRegistered()) {
85 1
            throw new HandlerNotRegisteredException("ExceptionHandler");
86
        }
87 1
        restore_exception_handler();
88 1
        $this->isRegistered = false;
89 1
        return $this;
90
    }
91
92
    /**
93
     * Handles the exception thrown by the user or system.
94
     * Dispatch a log message containing the exception message.
95
     * @param \Exception $exception the exception thrown
96
     * @return \Brickoo\Component\Error\ExceptionHandler
97
     */
98 1
    public function handleException(\Exception $exception) {
99 1
        $this->messageDispatcher->dispatch(new ExceptionMessage($exception));
100 1
        return $this;
101
    }
102
103
}
104