Controller   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A _log() 0 13 1
A _info() 0 15 1
B _view() 0 38 3
1
<?php
2
3
/**
4
 * Handles errors and exceptions
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Errors
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\errors;
17
18
/**
19
 * Handles errors and exceptions
20
 *
21
 * @category  Core
22
 * @package   Errors
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Controller
30
{
31
    /**
32
     * Store service loader for later usage
33
     **/
34
    private $_loader = null;
35
36
    /**
37
     * Check and log exception details
38
     *
39
     * @param \Exception $exception Exception class object
40
     * @param boolean    $continue  Try to continue execution afterwards
41
     *
42
     * @return \csphere\core\errors\Controller
43
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
44
45
    public function __construct(\Exception $exception, $continue = false)
46
    {
47
        // Create loader object
48
        $this->_loader = \csphere\core\service\Locator::get();
49
50
        // Log exception details
51
        $this->_log($exception);
52
53
        // Handle displayed details if continue is false
54
        if ($continue == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
55
56
            $this->_view($exception);
57
        }
58
    }
59
60
    /**
61
     * Logs errors and exceptions that occur
62
     *
63
     * @param \Exception $exception Exception class object
64
     *
65
     * @return void
66
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
67
68
    private function _log(\Exception $exception)
69
    {
70
        $msg = 'Message: ' . $exception->getMessage() . "\n"
71
             . 'Code: ' . $exception->getCode() . "\n"
72
             . 'File: ' . $exception->getFile() . "\n"
73
             . 'Line: ' . $exception->getLine();
74
75
        $append = "\n" . 'Trace:' . "\n" . $exception->getTraceAsString();
76
77
        $log = $this->_loader->load('logs');
78
79
        $log->log('errors', $msg, true, $append);
80
    }
81
82
    /**
83
     * Return the string to attach to the template
84
     *
85
     * @return string
86
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
87
88
    private function _info()
89
    {
90
        // Get view object
91
        $view = $this->_loader->load('view');
92
93
        // Format data for template usage
94
        $data = [];
95
96
        // Send data to view and fetch box result
97
        $view->template('errors', 'core_info', $data, true);
98
99
        $result = $view->box();
100
101
        return $result;
102
    }
103
104
    /**
105
     * Care about what will be shown
106
     *
107
     * @param \Exception $exception Exception class object
108
     *
109
     * @return void
110
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
111
112
    private function _view(\Exception $exception)
113
    {
114
        // Create a view
115
        $template = $this->_loader->load('view');
116
117
        // Check if debug is enabled
118
        $check = $template->getOption('debug');
119
120
        if (empty($check)) {
121
122
            // Message to display when debug is turned off
123
            $content = $this->_info();
124
125
            // Update route target
126
            \csphere\core\template\Hooks::route('errors', 'info');
127
128
        } else {
129
130
            // Message to display when debug is turned on
131
            $backtrace = new \csphere\core\debug\Backtrace($exception);
132
133
            $content = $backtrace->content();
134
135
            // Update route target
136
            \csphere\core\template\Hooks::route('debug', 'backtrace');
137
        }
138
139
        // If there is content display it
140
        if ($content != '') {
141
142
            $template->add($content);
143
144
            // Load router without search and skip target file
145
            $router = new \csphere\core\router\Controller();
146
147
            $router->execute(true);
148
        }
149
    }
150
}
151