Passed
Push — 6.0 ( 2749ab...de6c84 )
by liu
02:35
created

SessionInit   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 22 5
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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\Session;
19
20
class SessionInit
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
21
{
22
23
    /** @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...
24
    protected $session;
25
26
    /** @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...
27
    protected $app;
28
29
    public function __construct(App $app, Session $session)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
30
    {
31
        $this->app     = $app;
32
        $this->session = $session;
33
    }
34
35
    /**
36
     * Session初始化
37
     * @access public
38
     * @param Request $request
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
39
     * @param Closure $next
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
40
     * @return void
41
     */
42
    public function handle($request, Closure $next)
43
    {
44
        // Session初始化
45
        $varSessionId = $this->app->config->get('route.var_session_id');
46
47
        if ($varSessionId && $request->request($varSessionId)) {
48
            $this->session->setId($request->request($varSessionId));
0 ignored issues
show
Bug introduced by
$request->request($varSessionId) of type array|null|object is incompatible with the type string expected by parameter $id of think\Session::setId(). ( Ignorable by Annotation )

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

48
            $this->session->setId(/** @scrutinizer ignore-type */ $request->request($varSessionId));
Loading history...
49
        } else {
50
            $cookieName = $this->app->config->get('session.cookie_name', 'PHPSESSID');
51
            $sessionId  = $request->cookie($cookieName) ?: '';
0 ignored issues
show
Bug introduced by
It seems like $cookieName can also be of type array and array; however, parameter $name of think\Request::cookie() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

51
            $sessionId  = $request->cookie(/** @scrutinizer ignore-type */ $cookieName) ?: '';
Loading history...
52
            $this->session->setId($sessionId);
53
        }
54
55
        $request->withSession($this->session);
56
57
        $response = $next($request);
58
59
        if (isset($cookieName)) {
60
            $this->app->cookie->set($cookieName, $this->session->getId());
61
        }
62
63
        return $response;
64
    }
65
}
66