Completed
Push — 6.0 ( cb8817...411736 )
by liu
08:19
created

Redirect::with()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\response;
14
15
use think\Request;
16
use think\Response;
17
use think\Session;
18
19
/**
20
 * Redirect Response
21
 */
22
class Redirect extends Response
23
{
24
25
    protected $request;
26
27
    public function __construct(Request $request, Session $session, $data = '', int $code = 302)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
28
    {
29
        parent::__construct((string) $data, $code);
30
31
        $this->request = $request;
32
        $this->session = $session;
33
34
        $this->cacheControl('no-cache,must-revalidate');
35
    }
36
37
    /**
38
     * 处理数据
39
     * @access protected
40
     * @param  mixed $data 要处理的数据
41
     * @return string
42
     */
43
    protected function output($data): string
44
    {
45
        $this->header['Location'] = $data;
46
47
        return '';
48
    }
49
50
    /**
51
     * 重定向传值(通过Session)
52
     * @access protected
53
     * @param  string|array  $name 变量名或者数组
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
54
     * @param  mixed         $value 值
55
     * @return $this
56
     */
57
    public function with($name, $value = null)
58
    {
59
        if (is_array($name)) {
60
            foreach ($name as $key => $val) {
61
                $this->session->flash($key, $val);
0 ignored issues
show
Bug introduced by
The method flash() does not exist on think\Session. Since you implemented __call, 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

61
                $this->session->/** @scrutinizer ignore-call */ 
62
                                flash($key, $val);
Loading history...
62
            }
63
        } else {
64
            $this->session->flash($name, $value);
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * 记住当前url后跳转
72
     * @access public
73
     * @return $this
74
     */
75
    public function remember()
76
    {
77
        $this->session->set('redirect_url', $this->request->url());
0 ignored issues
show
Bug introduced by
The method set() does not exist on think\Session. Since you implemented __call, 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

77
        $this->session->/** @scrutinizer ignore-call */ 
78
                        set('redirect_url', $this->request->url());
Loading history...
78
79
        return $this;
80
    }
81
82
    /**
83
     * 跳转到上次记住的url
84
     * @access public
85
     * @return $this
86
     */
87
    public function restore()
88
    {
89
        if ($this->session->has('redirect_url')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on think\Session. Since you implemented __call, 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

89
        if ($this->session->/** @scrutinizer ignore-call */ has('redirect_url')) {
Loading history...
90
            $this->data = $this->session->get('redirect_url');
0 ignored issues
show
Bug introduced by
The method get() does not exist on think\Session. Since you implemented __call, 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

90
            /** @scrutinizer ignore-call */ 
91
            $this->data = $this->session->get('redirect_url');
Loading history...
91
            $this->session->delete('redirect_url');
0 ignored issues
show
Bug introduced by
The method delete() does not exist on think\Session. Since you implemented __call, 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

91
            $this->session->/** @scrutinizer ignore-call */ 
92
                            delete('redirect_url');
Loading history...
92
        }
93
94
        return $this;
95
    }
96
}
97