Completed
Push — master ( 085f18...5909bf )
by Andrii
19:10
created

ClientQuery   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 86
ccs 0
cts 74
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 29 1
B andWhere() 0 17 6
A whereId() 0 4 1
A whereEmail() 0 4 1
A whereUsername() 0 9 2
A wherePassword() 0 7 1
A whereActive() 0 7 3
1
<?php
2
3
/*
4
 * HIAM module for MRDP database compatibility
5
 *
6
 * @link      https://github.com/hiqdev/hiam-mrdp
7
 * @package   hiam-mrdp
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiam\mrdp\storage;
13
14
class ClientQuery extends \yii\db\ActiveQuery
15
{
16
    public function init()
17
    {
18
        parent::init();
19
        $this
20
            ->select([
21
                'c.obj_id       AS id',
22
                'c.login        AS username',
23
                'r.login        AS seller',
24
                'c.seller_id    AS seller_id',
25
                'y.name         AS type',
26
                'z.name         AS state',
27
                'e.roles        AS roles',
28
                'k.first_name   AS first_name',
29
                'k.last_name    AS last_name',
30
                't.value        AS totp_secret',
31
                'coalesce(i.value,l.value) AS allowed_ips',
32
                'coalesce(c.email,k.email) AS email',
33
            ])
34
            ->from('zclient             c')
35
            ->innerJoin('zclient        r', 'r.obj_id=c.seller_id')
36
            ->innerJoin('ref            y', 'y.obj_id=c.type_id')
37
            ->innerJoin('ref            z', "z.obj_id=c.state_id AND z.name IN ('ok', 'active', 'new')")
38
            ->leftJoin('contact         k', 'k.obj_id=c.obj_id')
39
            ->leftJoin('value           t', "t.obj_id=c.obj_id AND t.prop_id=prop_id('client,access:totp_secret')")
40
            ->leftJoin('value           i', "i.obj_id=c.obj_id AND i.prop_id=prop_id('client,access:allowed_ips')")
41
            ->leftJoin('value           l', "l.obj_id=c.obj_id AND l.prop_id=prop_id('login_ips:panel')")
42
            ->leftJoin('client2rolez    e', 'e.client_id=c.obj_id')
43
        ;
44
    }
45
46
    public function andWhere($condition)
47
    {
48
        if (!is_array($condition) || $condition[0]) {
49
            return parent::andWhere($condition);
50
        }
51
        foreach (['id', 'username', 'password', 'email', 'active'] as $key) {
52
            if (isset($condition[$key])) {
53
                $this->{"where$key"}($condition[$key]);
54
                unset($condition[$key]);
55
            }
56
        }
57
        if (!empty($condition)) {
58
            $this->andWhere($condition);
59
        }
60
61
        return $this;
62
    }
63
64
    public function whereId($id)
65
    {
66
        return parent::andWhere(['c.obj_id' => $id]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of whereId()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
67
    }
68
69
    public function whereEmail($username)
70
    {
71
        return $this->whereUsername($username);
72
    }
73
74
    public function whereUsername($username)
75
    {
76
        $userId = (int) $username;
77
        if ($userId > 0) {
78
            return $this->whereId($userId);
79
        }
80
81
        return parent::andWhere(['or', 'c.login=:username', 'c.email=:username'], [':username' => $username]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of whereUsername()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
82
    }
83
84
    public function wherePassword($password)
85
    {
86
        return parent::andWhere(
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of wherePassword()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
87
            'check_password(:password,c.password) OR check_password(:password,tmp.value)',
88
            [':password' => $password]
89
        )->leftJoin('value tmp', "tmp.obj_id=c.obj_id AND tmp.prop_id=prop_id('client,access:tmp_pwd')");
90
    }
91
92
    public function whereActive($is_active)
93
    {
94
        if (is_null($is_active)) {
95
            return $this;
96
        }
97
        return parent::andWhere([$is_active ? 'in' : 'not in', 'z.name', ['ok', 'active']]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (andWhere() instead of whereActive()). Are you sure this is correct? If so, you might want to change this to $this->andWhere().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
98
    }
99
}
100