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

SessionHistoryQuery::whereCurrentUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Query;
13
14
use Da\User\Traits\ModuleAwareTrait;
15
use yii\db\ActiveQuery;
16
use Yii;
17
18
class SessionHistoryQuery extends ActiveQuery
19
{
20
    use ModuleAwareTrait;
21
22
    public function whereUserId($userId)
23
    {
24
        return $this->andWhere($this->getCondition()->byUser($userId));
25
    }
26
27
    public function whereActive()
28
    {
29
        return $this->andWhere(['IS NOT', 'session_id', null]);
30
    }
31
32
    public function whereInActive($userId)
33
    {
34
        return $this->andWhere($this->getCondition()->inactive($userId));
35
    }
36
37
38
    public function whereExpired($userId)
39
    {
40
        return $this->andWhere($this->getCondition()->expired($userId));
41
    }
42
43
    public function whereExpiredInActive($userId)
44
    {
45
        return $this->andWhere($this->getCondition()->expiredInactive($userId));
46
    }
47
48
    public function selectSessionId()
49
    {
50
        return $this->select(['session_id']);
51
    }
52
53
    public function whereUserSession($userId, $sessionId)
54
    {
55
        return $this->andWhere($this->getCondition()->byUserSession(
56
            $userId,
57
            $sessionId
58
        ));
59
    }
60
61
    public function whereCurrentUser()
62
    {
63
        return $this->andWhere($this->getCondition()->currentUserCondition());
64
    }
65
66
    public function oldestUpdatedTimeActiveSession($userId)
67
    {
68
        return $this->whereExpiredInActive($userId)
69
            ->select(['updated_at'])
70
            ->limit(1)
71
            ->offset($this->getModule()->numberSessionHistory)
0 ignored issues
show
Bug introduced by
It seems like $this->getModule()->numberSessionHistory can also be of type boolean; however, yii\db\QueryTrait::offset() does only seem to accept integer|object<yii\db\ExpressionInterface>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
72
            ->orderBy(['updated_at' => SORT_DESC])->scalar();
73
    }
74
75
    /**
76
     * @return SessionHistoryCondition
77
     */
78
    protected function getCondition()
79
    {
80
        return Yii::$container->get(SessionHistoryCondition::class);
81
    }
82
}
83