Completed
Push — 6.0 ( 9db11b...892b87 )
by yun
06:42
created

SessionInit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 2
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\middleware;
14
15
use Closure;
16
use think\App;
17
use think\Request;
18
use think\Response;
19
use think\Session;
20
21
/**
22
 * Session初始化
23
 */
24
class SessionInit
25
{
26
27
    /** @var App */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
28
    protected $app;
29
30
    /** @var Session */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
31
    protected $session;
32
33
    public function __construct(App $app, Session $session)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
34
    {
35
        $this->app     = $app;
36
        $this->session = $session;
37
    }
38
39
    /**
40
     * Session初始化
41
     * @access public
42
     * @param Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
43
     * @param Closure $next
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     * @return Response
45
     */
46
    public function handle($request, Closure $next)
47
    {
48
        // Session初始化
49
        $varSessionId = $this->app->config->get('session.var_session_id');
50
        $cookieName   = $this->session->getName();
0 ignored issues
show
Bug introduced by
The method getName() 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

50
        /** @scrutinizer ignore-call */ 
51
        $cookieName   = $this->session->getName();
Loading history...
51
52
        if ($varSessionId && $request->request($varSessionId)) {
53
            $sessionId = $request->request($varSessionId);
54
        } else {
55
            $sessionId = $request->cookie($cookieName);
56
        }
57
58
        $this->session->setId($sessionId);
0 ignored issues
show
Bug introduced by
The method setId() 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

58
        $this->session->/** @scrutinizer ignore-call */ 
59
                        setId($sessionId);
Loading history...
59
        $this->session->init();
0 ignored issues
show
Bug introduced by
The method init() 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

59
        $this->session->/** @scrutinizer ignore-call */ 
60
                        init();
Loading history...
60
61
        $request->withSession($this->session);
62
63
        /** @var Response $response */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
64
        $response = $next($request);
65
66
        $response->setSession($this->session);
67
68
        $this->app->cookie->set($cookieName, $this->session->getId());
0 ignored issues
show
Bug introduced by
The method getId() 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

68
        $this->app->cookie->set($cookieName, $this->session->/** @scrutinizer ignore-call */ getId());
Loading history...
69
70
        return $response;
71
    }
72
73
    public function end(Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

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

73
    public function end(/** @scrutinizer ignore-unused */ Response $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing doc comment for function end()
Loading history...
74
    {
75
        $this->session->save();
0 ignored issues
show
Bug introduced by
The method save() 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

75
        $this->session->/** @scrutinizer ignore-call */ 
76
                        save();
Loading history...
76
    }
77
}
78