Response   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setHeaderContentType() 0 12 2
A redirect() 0 11 3
1
<?php
2
3
namespace Core\Dependency;
4
5
/**
6
 * Creating all the response messages
7
 * Class Response
8
 * @package Core\Dependency
9
 */
10
class Response
11
{
12
13
    /**
14
     * Setting the header type. useful for Json returns
15
     * @param string $type
16
     */
17
    public function setHeaderContentType(string $type):void
18
    {
19
        switch ($type) {
20
            case 'json':
21
                $headerType = 'application/json';
22
                break;
23
            default:
24
                $headerType = 'text/html';
25
        }
26
        $contentType = 'Content-Type: ' . $headerType;
27
28
        header($contentType);
29
    }
30
31
    /**
32
     * redirects the user to a different page
33
     * @param string $url
34
     */
35
    public function redirect(string $url = ''): void
36
    {
37
        //if the url was passed with a forward slash, remove it as it will be added later.
38
        if ($url !== '') {
39
            if ($url[0] === '/') {
40
                $url = substr($url, 1);
41
            }
42
        }
43
44
        header("location: /" . $url);
45
        exit(); //after redirect do not execute anything else from the function
46
    }
47
}