ErrorHandler::execute()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 3
nop 2
crap 2
1
<?php
2
3
/**
4
 * Copyright 2014 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2014 Fabian Grutschus. All rights reserved.
33
 * @license   BSD
34
 * @link      http://github.com/fabiang/xmpp
35
 */
36
37
namespace Fabiang\Xmpp\Util;
38
39
use Fabiang\Xmpp\Exception\InvalidArgumentException;
40
use Fabiang\Xmpp\Exception\ErrorException;
41
42
/**
43
 * XML utility methods.
44
 *
45
 * @package Xmpp\Util
46
 */
47
class ErrorHandler
48
{
49
50
    /**
51
     * Method to be called.
52
     *
53
     * @var callable
54
     */
55
    protected $method;
56
57
    /**
58
     * Arguments for method.
59
     *
60
     * @var array
61
     */
62
    protected $arguments = [];
63
64 9
    public function __construct($method)
65
    {
66 9
        if (!is_callable($method)) {
67 3
            throw new InvalidArgumentException('Argument 1 of "' . __METHOD__ . '" must be a callable');
68
        }
69
70 9
        $arguments = func_get_args();
71 9
        array_shift($arguments);
72
73 9
        $this->method    = $method;
74 9
        $this->arguments = $arguments;
75 9
    }
76
77
    /**
78
     * Execute a function and handle all types of errors.
79
     *
80
     * @param string $file
81
     * @param int    $line
82
     * @return mixed
83
     * @throws ErrorException
84
     */
85
    public function execute($file, $line)
86
    {
87 6
        set_error_handler(function ($errno, $errstr) use ($file, $line) {
88 3
            throw new ErrorException($errstr, 0, $errno, $file, $line);
89 6
        });
90
91
        try {
92 6
            $value = call_user_func_array($this->method, $this->arguments);
93 3
            restore_error_handler();
94 3
            return $value;
95 3
        } catch (ErrorException $exception) {
96 3
            restore_error_handler();
97 3
            throw $exception;
98
        }
99
    }
100
}
101