Passed
Branch v1.5.1 (9f4477)
by Wanderson
01:49
created

Application::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Win;
4
5
use App\Controllers\IndexController;
6
use Win\Controllers\Controller;
7
use Win\Repositories\Database\Connection;
8
use Win\Request\Url;
9
use Win\HttpException;
10
use Win\Views\View;
11
12
/**
13
 * Application (WinPHP Framework)
14
 *
15
 * Framework em PHP baseado em MVC
16
 * Responsável por incluir as páginas de acordo com a URL e criar a estrutura MVC
17
 * @author winPHP Framework <http://github.com/winframework/winphp/>
18
 * @version 1.6.0
19
 */
20
class Application
21
{
22
	public Controller $controller;
23
	public View $view;
24
	public Connection $conn;
25
	protected static Application $instance;
26
27
	/**
28
	 * Cria a aplicação principal
29
	 */
30
	public function __construct()
31
	{
32
		static::$instance = $this;
33
	}
34
35
	/**
36
	 * Retorna o ponteiro para a aplicação principal
37
	 * @return static
38
	 */
39
	public static function app()
40
	{
41
		return static::$instance;
42
	}
43
44
	/**
45
	 * Executa o Controller@action e envia o retorno como resposta
46
	 * @param string $class Controller
47
	 * @param string $method Action
48
	 * @param array $args
49
	 */
50
	public function run($class, $method, $args = [])
51
	{
52
		if (!class_exists($class)) {
53
			throw new HttpException("Controller '{$class}' not found", 404);
54
		}
55
56
		$controller = new $class($this);
57
		$controller->app = $this;
58
		$this->controller = $controller;
59
60
		if (!method_exists($controller, $method)) {
61
			throw new HttpException("Action '{$method}' not found in '{$class}'", 404);
62
		}
63
64
		$controller->init();
65
		$response = $controller->$method(...$args);
66
		echo $this->send($response);
67
	}
68
69
	/**
70
	 * Envia a resposta baseado no tipo
71
	 * @param mixed $response
72
	 * @return mixed
73
	 */
74
	private function send($response)
75
	{
76
		if (is_array($response)) {
77
			@header('Content-Type: application/json');
0 ignored issues
show
Bug introduced by
Are you sure the usage of header('Content-Type: application/json') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for header(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

77
			/** @scrutinizer ignore-unhandled */ @header('Content-Type: application/json');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
78
			return json_encode($response);
79
		}
80
81
		return $response;
82
	}
83
84
	/** @return string */
85
	public function getFullUrl()
86
	{
87
		return Url::instance()->getBaseUrl() . Url::instance()->getUrl();
88
	}
89
90
	/** @return string */
91
	public function getBaseUrl()
92
	{
93
		return Url::instance()->getBaseUrl();
94
	}
95
96
	/**
97
	 * Retorna a URL Atual
98
	 * @return string
99
	 */
100
	public function getUrl()
101
	{
102
		return Url::instance()->getUrl();
103
	}
104
105
	/**
106
	 * Retorna a página atual
107
	 * @return string
108
	 */
109
	public function getPage()
110
	{
111
		return Url::instance()->getSegments()[0];
112
	}
113
114
	/**
115
	 * Retorna TRUE se está na página inicial
116
	 * @return bool
117
	 */
118
	public function isHomePage()
119
	{
120
		return $this->controller instanceof IndexController;
121
	}
122
123
	/**
124
	 * Define a página como 404
125
	 * @param string $message
126
	 * @throws HttpException
127
	 */
128
	public function page404($message = '')
129
	{
130
		throw new HttpException($message, 404);
131
	}
132
133
	/**
134
	 * Define a página atual como algum erro
135
	 * @param int $code
136
	 * @param string $message
137
	 * @throws HttpException
138
	 */
139
	public function errorPage($code, $message = '')
140
	{
141
		throw new HttpException($message, $code);
142
	}
143
}
144