Completed
Push — master ( ee49f4...f8fced )
by Alejandro
02:23
created

Tight::printException()   C

Complexity

Conditions 7
Paths 33

Size

Total Lines 49
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 49
rs 6.7272
cc 7
eloc 19
nc 33
nop 1
1
<?php
2
3
namespace Tight;
4
5
/*
6
 * The MIT License
7
 *
8
 * Copyright 2016 Alejandro Peña Florentín ([email protected])
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
29
/**
30
 * Tight framework main class
31
 *
32
 * @author Alejandro Peña Florentín ([email protected])
33
 */
34
class Tight
35
{
36
37
    /**
38
     * Current Version
39
     */
40
    const VERSION = "v1.0.1";
41
42
    private static $INSTANCE = null;
43
44
    /**
45
     *
46
     * @var Tight\TightConfig Configuration
47
     */
48
    private $config = null;
49
50
    /**
51
     *
52
     * @var Tight\Router 
53
     */
54
    private $router;
55
    private $smarty;
56
57
    /**
58
     * Creates an instance of Tight Framework.
59
     * @param array $config Settings to override the config file
60
     */
61
    public function __construct($config = []) {
62
        set_exception_handler([$this, "exceptionHandler"]);
63
        $this->setConfig($config);
64
        $this->router = new Router($this->config->basePath);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Tight\Router($this->config->basePath) of type object<Tight\Router> is incompatible with the declared type object<Tight\Tight\Router> of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
        $this->smarty = new \Smarty;
66
        $this->smarty->template_dir = $this->config->smarty["template_dir"];
67
        $this->smarty->compile_dir = $this->config->smarty["compile_dir"];
68
        $this->smarty->config_dir = $this->config->smarty["config_dir"];
69
        $this->smarty->cache_dir = $this->config->smarty["cache_dir"];
70
    }
71
72
    public static function getInstance() {
73
        if (null !== self::$INSTANCE) {
74
            return self::$INSTANCE;
75
        } else {
76
            return new \Tight\Tight;
77
        }
78
    }
79
80
    public function setConfig($config) {
81
        if (is_array($config)) {
82
            $config = new \Tight\TightConfig($config);
83
        } elseif (!$config instanceof \Tight\TightConfig) {
84
            throw new \InvalidArgumentException("Argument passed to Tight::__constructor must be array or <b>\Tight\TightConfig</b> object");
85
        }
86
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<Tight\TightConfig> is incompatible with the declared type object<Tight\Tight\TightConfig> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
    }
88
89
    public function getConfig() {
90
        return $this->config;
91
    }
92
93
    /**
94
     * 
95
     * @return \Smarty
96
     */
97
    public function getSmarty() {
98
        return $this->smarty;
99
    }
100
101
    public function exceptionHandler($ex) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ex. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
102
        self::printException($ex);
103
    }
104
105
    /**
106
     * Custom exception handler
107
     * @param \Exception $ex Exception
108
     * @codeCoverageIgnore
109
     */
110
    public static function printException($ex) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ex. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
111
        $lastTrace = $ex->getTrace()[count($ex->getTrace()) - 1];
112
        $output = <<<EXC
113
                <!DOCTYPE>
114
                <html>
115
                    <head>
116
                        <title>Tight Framework Exception</title>
117
                        <style>
118
                        *{
119
                            margin: 0;
120
                            padding: 0;
121
                        }
122
                        html {
123
                            font-family: Helvetica;
124
                        }
125
                        body {
126
                            padding: 1em;
127
                        }
128
                        h1 {
129
                            margin-bottom: 0.5em;
130
                        }
131
                        p {
132
                            margin-top: 0.25em;
133
                            margin-bottom: 0.5em;
134
                        }
135
                        .padding-left {
136
                            padding-left: 2em;
137
                        }
138
                        </style>
139
                    </head>
140
                    <body>
141
                        <h1>Tight Framework Exception</h1>
142
EXC;
143
        $output .= "<p><strong>" . get_class($ex) . ": </strong>" . $ex->getMessage() . "</p>";
144
        $output .= "<p class='padding-left'>in <strong>" . $lastTrace['file'] . "</strong> at line <strong>" . $lastTrace['line'] . "</strong></p>";
145
        $output .= "<br/>";
146
        $output .= "<p>Stack Trace:</p>";
147
        $trace = $ex->getTrace();
148
        for ($index = 0; $index < count($trace); $index++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
149
            $el = $trace[$index];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $el. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
150
            $class = isset($el["class"]) ? $el["class"] : "";
151
            $type = isset($el["type"]) ? $el["type"] : "";
152
            $func = isset($el["function"]) ? $el["function"] : "";
153
            $file = isset($el["file"]) ? "at <strong>" . $el["file"] . "</strong>" : "";
154
            $line = isset($el["line"]) ? " at line <strong>" . $el["line"] . "</strong>" : "";
155
            $output .= "<p>#" . ($index + 1) . ": " . $class . $type . $func . "() " . $file . $line . "</strong></p>";
156
        }
157
        echo $output;
158
    }
159
160
    /**
161
     * 
162
     * @return \Tight\Router
0 ignored issues
show
Documentation introduced by
Should the return type not be Tight\Router?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
163
     */
164
    public function getRouter() {
165
        return $this->router;
166
    }
167
168
    /**
169
     * Runs the router to send response to the request URI
170
     */
171
    public function run() {
172
        if ($this->config->mvc["enableRouter"]) {
173
            $this->router->runMvc();
174
        } else {
175
            $this->router->run();
176
        }
177
    }
178
179
}
180