Passed
Push — master ( 5b6f7f...57f3ff )
by Melech
03:58
created

Abort::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Routing\Support;
15
16
use Valkyrja\Http\Message\Constant\StatusText;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\Constant\StatusText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Http\Message\Enum\StatusCode;
18
use Valkyrja\Http\Message\Exception\HttpException;
19
use Valkyrja\Http\Message\Exception\HttpRedirectException;
20
use Valkyrja\Http\Message\Response\Contract\Response;
21
use Valkyrja\Http\Message\Uri\Contract\Uri;
22
23
/**
24
 * Class Abort.
25
 *
26
 * @author Melech Mizrachi
27
 */
28
class Abort
29
{
30
    /**
31
     * Abort with a 400.
32
     *
33
     * @param array<string, string[]>|null $headers  [optional] The headers
34
     * @param Response|null                $response [optional] The response to send
35
     *
36
     * @return never
37
     */
38
    public static function abort400(array|null $headers = null, Response|null $response = null): never
39
    {
40
        static::abort(StatusCode::BAD_REQUEST, StatusText::BAD_REQUEST, $headers, $response);
41
    }
42
43
    /**
44
     * Abort with a 404.
45
     *
46
     * @param array<string, string[]>|null $headers  [optional] The headers
47
     * @param Response|null                $response [optional] The response to send
48
     *
49
     * @return never
50
     */
51
    public static function abort404(array|null $headers = null, Response|null $response = null): never
52
    {
53
        static::abort(StatusCode::NOT_FOUND, StatusText::NOT_FOUND, $headers, $response);
54
    }
55
56
    /**
57
     * Abort with a 405.
58
     *
59
     * @param array<string, string[]>|null $headers  [optional] The headers
60
     * @param Response|null                $response [optional] The response to send
61
     *
62
     * @return never
63
     */
64
    public static function abort405(array|null $headers = null, Response|null $response = null): never
65
    {
66
        static::abort(StatusCode::METHOD_NOT_ALLOWED, StatusText::METHOD_NOT_ALLOWED, $headers, $response);
67
    }
68
69
    /**
70
     * Abort with a 413.
71
     *
72
     * @param array<string, string[]>|null $headers  [optional] The headers
73
     * @param Response|null                $response [optional] The response to send
74
     *
75
     * @return never
76
     */
77
    public static function abort413(array|null $headers = null, Response|null $response = null): never
78
    {
79
        static::abort(StatusCode::PAYLOAD_TOO_LARGE, StatusText::PAYLOAD_TOO_LARGE, $headers, $response);
80
    }
81
82
    /**
83
     * Abort.
84
     *
85
     * @param StatusCode|null              $statusCode [optional] The status code
86
     * @param string|null                  $message    [optional] The message
87
     * @param array<string, string[]>|null $headers    [optional] The headers
88
     * @param Response|null                $response   [optional] The response to send
89
     *
90
     * @return never
91
     */
92
    public static function abort(
93
        StatusCode|null $statusCode = null,
94
        string|null $message = null,
95
        array|null $headers = null,
96
        Response|null $response = null
97
    ): never {
98
        throw new HttpException($statusCode, $message, $headers, $response);
99
    }
100
101
    /**
102
     * Redirect to a given uri, and abort.
103
     *
104
     * @param Uri|null                     $uri        [optional] The URI to redirect to
105
     * @param StatusCode|null              $statusCode [optional] The response status code
106
     * @param array<string, string[]>|null $headers    [optional] An array of response headers
107
     *
108
     * @throws HttpRedirectException
109
     *
110
     * @return never
111
     */
112
    public static function redirect(
113
        Uri|null $uri = null,
114
        StatusCode|null $statusCode = null,
115
        array|null $headers = null
116
    ): never {
117
        throw new HttpRedirectException($uri, $statusCode, $headers);
118
    }
119
}
120