Passed
Pull Request — master (#303)
by Arman
02:44
created

Header   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasHeader() 0 3 1
A getHeader() 0 3 2
A deleteHeader() 0 4 2
A getContentType() 0 3 1
A setContentType() 0 3 1
A allHeaders() 0 3 1
A setHeader() 0 3 1
A redirect() 0 5 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.8
13
 */
14
15
namespace Quantum\Http\Traits\Response;
16
17
use Quantum\App\Exceptions\StopExecutionException;
18
use Quantum\Http\Constants\ContentType;
19
20
/**
21
 * Trait Header
22
 * @package Quantum\Http\Response
23
 */
24
trait Header
25
{
26
27
    /**
28
     * Response headers
29
     * @var array
30
     */
31
    private static $__headers = [];
32
33
    /**
34
     * Checks the response header existence by given key
35
     * @param string $key
36
     * @return bool
37
     */
38
    public static function hasHeader(string $key): bool
39
    {
40
        return isset(self::$__headers[$key]);
41
    }
42
43
    /**
44
     * Gets the response header by given key
45
     * @param string $key
46
     * @return string|null
47
     */
48
    public static function getHeader(string $key): ?string
49
    {
50
        return self::hasHeader($key) ? self::$__headers[$key] : null;
51
    }
52
53
    /**
54
     * Sets the response header
55
     * @param string $key
56
     * @param string $value
57
     */
58
    public static function setHeader(string $key, string $value)
59
    {
60
        self::$__headers[$key] = $value;
61
    }
62
63
    /**
64
     * Get all response headers
65
     * @return array
66
     */
67
    public static function allHeaders(): array
68
    {
69
        return self::$__headers;
70
    }
71
72
    /**
73
     * Deletes the header by given key
74
     * @param string $key
75
     */
76
    public static function deleteHeader(string $key)
77
    {
78
        if (self::hasHeader($key)) {
79
            unset(self::$__headers[$key]);
80
        }
81
    }
82
83
    /**
84
     * Sets the content type
85
     * @param string $contentType
86
     */
87
    public static function setContentType(string $contentType)
88
    {
89
        self::setHeader('Content-Type', $contentType);
90
    }
91
92
    /**
93
     * Gets the content type
94
     * @return string|null
95
     */
96
    public static function getContentType(): string
97
    {
98
        return self::getHeader('Content-Type') ?? ContentType::HTML;
99
    }
100
101
    /**
102
     * Redirect
103
     * @param string $url
104
     * @param int $code
105
     * @throws StopExecutionException
106
     */
107
    public static function redirect(string $url, int $code = 302)
108
    {
109
        self::setStatusCode($code);
110
        self::setHeader('Location', $url);
111
        stop();
112
    }
113
}