Passed
Pull Request — master (#395)
by
unknown
03:36
created

nav_ref_decode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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 3.0.0
13
 */
14
15
use Quantum\Config\Exceptions\ConfigException;
16
use Quantum\App\Exceptions\BaseException;
17
use Quantum\Di\Exceptions\DiException;
18
use Quantum\App\Enums\ReservedKeys;
19
use Quantum\Http\Enums\ContentType;
20
use Quantum\Http\Enums\StatusCode;
21
use DebugBar\DebugBarException;
22
use Quantum\Http\Response;
23
use Quantum\Http\Request;
24
25
/**
26
 * Gets the base url
27
 * @param bool $withModulePrefix
28
 * @return string
29
 */
30
function base_url(bool $withModulePrefix = false): string
31
{
32
    return Request::getBaseUrl($withModulePrefix);
0 ignored issues
show
Bug introduced by
The method getBaseUrl() does not exist on Quantum\Http\Request. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

32
    return Request::/** @scrutinizer ignore-call */ getBaseUrl($withModulePrefix);
Loading history...
33
}
34
35
/**
36
 * Gets the current url
37
 * @return string
38
 */
39
function current_url(): string
40
{
41
    return Request::getCurrentUrl();
0 ignored issues
show
Bug introduced by
The method getCurrentUrl() does not exist on Quantum\Http\Request. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

41
    return Request::/** @scrutinizer ignore-call */ getCurrentUrl();
Loading history...
42
}
43
44
/**
45
 * Redirect
46
 * @param string $url
47
 * @param int $code
48
 */
49
function redirect(string $url, int $code = StatusCode::FOUND)
50
{
51
    Response::redirect($url, $code);
52
}
53
54
/**
55
 * Redirect with data
56
 * @param string $url
57
 * @param array $data
58
 * @param int $code
59
 * @return void
60
 * @throws BaseException
61
 * @throws ConfigException
62
 * @throws DiException
63
 * @throws ReflectionException
64
 */
65
function redirectWith(string $url, array $data, int $code = StatusCode::FOUND)
66
{
67
    session()->set(ReservedKeys::PREV_REQUEST, $data);
68
    Response::redirect($url, $code);
69
}
70
71
/**
72
 * Gets old input values after redirect
73
 * @param string $key
74
 * @return mixed|null
75
 * @throws ConfigException
76
 * @throws DiException
77
 * @throws ReflectionException
78
 * @throws BaseException
79
 */
80
function old(string $key)
81
{
82
    if (!session()->has(ReservedKeys::PREV_REQUEST)) {
83
        return null;
84
    }
85
86
    $prevRequest = session()->get(ReservedKeys::PREV_REQUEST);
87
88
    if (!is_array($prevRequest) || !isset($prevRequest[$key])) {
89
        return null;
90
    }
91
92
    $value = $prevRequest[$key];
93
    unset($prevRequest[$key]);
94
95
    session()->set(ReservedKeys::PREV_REQUEST, $prevRequest);
96
97
    return $value;
98
}
99
100
/**
101
 * Gets the referrer
102
 * @return string|null
103
 */
104
function get_referrer(): ?string
105
{
106
    return Request::getReferrer();
107
}
108
109
/**
110
 * Handles page not found
111
 * @return void
112
 * @throws BaseException
113
 * @throws ConfigException
114
 * @throws DiException
115
 * @throws ReflectionException
116
 * @throws DebugBarException
117
 */
118
function page_not_found()
119
{
120
    $acceptHeader = Response::getHeader('Accept');
121
122
    $isJson = $acceptHeader === ContentType::JSON;
123
124
    if ($isJson) {
125
        Response::json(
126
            ['status' => 'error', 'message' => 'Page not found'],
127
            StatusCode::NOT_FOUND
128
        );
129
    } else {
130
        Response::html(
131
            partial('errors' . DS . StatusCode::NOT_FOUND),
132
            StatusCode::NOT_FOUND
133
        );
134
    }
135
}
136
137
/**
138
 * Encodes current query string to URL-safe base64.
139
 *
140
 * @return string
141
 */
142
function nav_ref_encode(): string
143
{
144
    $q = Request::getQuery();
145
    return $q ? rtrim(strtr(base64_encode($q), '+/', '-_'), '=') : '';
146
}
147
148
/**
149
 * Decodes a URL-safe base64 reference back to query string.
150
 *
151
 * @param string|null $ref
152
 * @return string|null
153
 */
154
function nav_ref_decode(?string $ref): ?string
155
{
156
    if (!$ref) return null;
157
    return base64_decode(strtr($ref, '-_', '+/')) ?: null;
158
}