Completed
Pull Request — master (#361)
by
unknown
03:09
created

TerminateSessionsService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 34
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 24 5
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Service\SessionHistory;
13
14
15
class TerminateSessionsService implements TerminateSessionsServiceInterface
16
{
17
    protected $sessionIds;
18
19
    public function __construct(array $sessionIds)
20
    {
21
        $this->sessionIds = $sessionIds;
22
    }
23
24
    public function run()
25
    {
26
        $currentSessionId = session_id();
27
        if (session_status() === PHP_SESSION_ACTIVE) {
28
            session_write_close();
29
        }
30
31
        foreach ($this->sessionIds as $sessionId) {
32
            if ($sessionId === $currentSessionId) {
33
                $currentSessionId = null;
34
            }
35
36
            session_id($sessionId);
37
            session_start();
38
            session_destroy();
39
        }
40
41
        if ($currentSessionId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $currentSessionId of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
42
            session_id($currentSessionId);
43
        }
44
        session_start();
45
46
        return true;
47
    }
48
}