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

TerminateSessionsService::run()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 20
cp 0
rs 9.2248
c 0
b 0
f 0
cc 5
nc 12
nop 0
crap 30
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
}