Passed
Push — master ( fca16e...a09e23 )
by Alexander
04:10
created

HttpResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 108
rs 10
c 1
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A withException() 0 5 1
A setProtocolVersion() 0 3 1
A throwResponse() 0 3 1
A header() 0 5 1
A status() 0 3 1
A content() 0 3 1
A getProtocolVersion() 0 3 1
1
<?php
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2023 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Components\Http\Resources;
24
25
use Throwable;
26
use Syscodes\Components\Http\Exceptions\HttpResponseException;
27
28
/**
29
 * Loads the response trait of headers, status code and content message.
30
 */
31
trait HttpResponse
32
{
33
    /**
34
     * The content of the response.
35
     * 
36
     * @var string $content
37
     */
38
    protected $content = null;
39
    
40
    /**
41
     * The exception that triggered the error response (if applicable).
42
     * 
43
     * @var \Exception|null $exception
44
     */
45
    protected $exception;
46
47
    /**
48
     * The Headers class instance.
49
     * 
50
     * @var \Syscodes\Components\Http\ResponseHeaders $headers
51
     */
52
	public $headers;
53
54
    /**
55
     * The HTTP protocol version.
56
     * 
57
     * @var string $version
58
     */
59
    protected $version;
60
61
    /**
62
     * Gets the content of the response.
63
     * 
64
     * @return string
65
     */
66
    public function content(): string
67
    {
68
        return $this->getContent();
0 ignored issues
show
Bug introduced by
It seems like getContent() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

68
        return $this->/** @scrutinizer ignore-call */ getContent();
Loading history...
69
    }
70
71
    /**
72
     * Gets the status code for the response.
73
     * 
74
     * @return int
75
     */
76
    public function status(): int
77
    {
78
        return $this->getStatusCode();
0 ignored issues
show
Bug introduced by
It seems like getStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

78
        return $this->/** @scrutinizer ignore-call */ getStatusCode();
Loading history...
79
    }
80
81
    /**
82
     * Sets a header on the response.
83
     * 
84
     * @param  string  $key  The header name
85
     * @param  string  $values  The value or an array of values
86
     * @param  bool  $replace  If you want to replace the value exists by the header
87
     * 
88
     * @return static
89
     */
90
    public function header($key, $values, $replace = true): static
91
    {
92
        $this->headers->set($key, $values, $replace);
93
94
        return $this;
95
    }
96
97
    /**
98
     * Gets the Http protocol version.
99
     * 
100
     * @return string
101
     */
102
    public function getProtocolVersion(): string
103
    {
104
        return $this->version;
105
    }
106
107
    /**
108
     * Sets the Http protocol version.
109
     * 
110
     * @return string
111
     */
112
    public function setProtocolVersion(string $version): void
113
    {
114
        $this->version = $version;
115
    }
116
117
    /**
118
     * Sets the exception to the response.
119
     * 
120
     * @param  \Throwable  $e
121
     * 
122
     * @return static
123
     */
124
    public function withException(Throwable $e): static
125
    {
126
        $this->exception = $e;
0 ignored issues
show
Documentation Bug introduced by
$e is of type Throwable, but the property $exception was declared to be of type Exception|null. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
127
128
        return $this;
129
    }
130
131
    /**
132
     * Throws the response in a HttpResponseException instance.
133
     * 
134
     * @throws \Syscodes\Components\Http\Exceptions\HttpResponseException
135
     */
136
    public function throwResponse()
137
    {
138
        throw new HttpResponseException($this);
139
    }
140
}