SessionConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ByJG\Session;
4
5
use ByJG\Util\JwtKeyInterface;
6
use ByJG\Util\JwtKeySecret;
7
use ByJG\Util\JwtRsaKey;
8
9
class SessionConfig
10
{
11
    protected $serverName;
12
13
    protected $sessionContext = 'default';
14
    protected $timeoutMinutes = 20;
15
    protected $cookieDomain = null;
16
    protected $cookiePath = '/';
17
    protected $jwtKey = null;
18
    protected $replaceSessionHandler = null;
19
20
    /**
21
     * SessionConfig constructor.
22
     * @param $serverName
23
     */
24
    public function __construct($serverName)
25
    {
26
        $this->serverName = $serverName;
27
    }
28
29
    public function withSessionContext($context) {
30
        $this->sessionContext = $context;
31
        return $this;
32
    }
33
34
    public function withTimeoutMinutes($timeout) {
35
        $this->timeoutMinutes = $timeout;
36
        return $this;
37
    }
38
39
    public function withTimeoutHours($timeout) {
40
        $this->timeoutMinutes = $timeout * 60;
0 ignored issues
show
Documentation Bug introduced by
It seems like $timeout * 60 can also be of type double. However, the property $timeoutMinutes is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
41
        return $this;
42
    }
43
44
    public function withCookie($domain, $path = "/") {
45
        $this->cookieDomain = $domain;
46
        $this->cookiePath = $path;
47
        return $this;
48
    }
49
50
    public function withSecret($secret) {
51
        $this->jwtKey = new JwtKeySecret($secret);
52
        return $this;
53
    }
54
    
55
    public function withRsaSecret($private, $public) {
56
        $this->jwtKey = new JwtRsaKey($private, $public);
57
        return $this;
58
    }
59
60
    public function replaceSessionHandler($startSession = true) {
61
        $this->replaceSessionHandler = $startSession;
62
        return $this;
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68
    public function getServerName()
69
    {
70
        return $this->serverName;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getSessionContext()
77
    {
78
        return $this->sessionContext;
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    public function getTimeoutMinutes()
85
    {
86
        return $this->timeoutMinutes;
87
    }
88
89
    /**
90
     * @return null
91
     */
92
    public function getCookieDomain()
93
    {
94
        return $this->cookieDomain;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getCookiePath()
101
    {
102
        return $this->cookiePath;
103
    }
104
105
    /**
106
     * @return JwtKeyInterface
107
     */
108
    public function getKey()
109
    {
110
        return $this->jwtKey;
111
    }
112
113
    public function isReplaceSession() {
114
        return $this->replaceSessionHandler !== null;
115
    }
116
117
    public function isStartSession() {
118
        return $this->replaceSessionHandler === true;
119
    }
120
}
121