ClientQuery   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 92
ccs 0
cts 81
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 33 1
A andWhere() 0 18 6
A whereId() 0 4 1
A whereEmail() 0 4 1
A whereUsername() 0 9 3
A wherePassword() 0 7 1
A whereActive() 0 8 3
1
<?php
2
/**
3
 * HIAM module for MRDP database compatibility
4
 *
5
 * @link      https://github.com/hiqdev/hiam-mrdp
6
 * @package   hiam-mrdp
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiam\mrdp\storage;
12
13
use yii\db\Query;
14
15
class ClientQuery extends \yii\db\ActiveQuery
16
{
17
    public function init()
18
    {
19
        parent::init();
20
        $this
21
            ->select([
22
                'c.obj_id       AS id',
23
                'c.login        AS username',
24
                'r.login        AS seller',
25
                'c.seller_id    AS seller_id',
26
                'y.name         AS type',
27
                'z.name         AS state',
28
                'e.roles        AS roles',
29
                'k.first_name   AS first_name',
30
                'k.last_name    AS last_name',
31
                't.value        AS totp_secret',
32
                'to_1(has_deposits(c.obj_id)) AS verified',
33
                'coalesce(i.value,l.value) AS allowed_ips',
34
                'coalesce(c.email,k.email) AS email',
35
                "encode(digest(c.password, 'sha1'), 'hex') AS password_hash",
36
                'o.value        AS email_confirmed'
37
            ])
38
            ->from('zclient             c')
39
            ->innerJoin('zclient        r', 'r.obj_id=c.seller_id')
40
            ->innerJoin('zref           y', 'y.obj_id=c.type_id')
41
            ->innerJoin('zref           z', 'z.obj_id=c.state_id')
42
            ->leftJoin('contact         k', 'k.obj_id=c.obj_id')
43
            ->leftJoin('value           t', "t.obj_id=c.obj_id AND t.prop_id=prop_id('client,access:totp_secret')")
44
            ->leftJoin('value           i', "i.obj_id=c.obj_id AND i.prop_id=prop_id('client,access:allowed_ips')")
45
            ->leftJoin('value           l', "l.obj_id=c.obj_id AND l.prop_id=prop_id('login_ips:panel')")
46
            ->leftJoin('value           o', "o.obj_id=c.obj_id AND o.prop_id=prop_id('contact:email_confirmed')")
47
            ->leftJoin('client2rolez    e', 'e.client_id=c.obj_id')
48
            ->andWhere(['in', 'z.name', ['ok', 'new']]);
49
    }
50
51
    public function andWhere($condition, $params = [])
52
    {
53
        if (!is_array($condition) || isset($condition[0])) {
54
            return parent::andWhere($condition, $params);
55
        }
56
        foreach (['id', 'username', 'password', 'email', 'active'] as $key) {
57
            /// XXX `isset` does not fit here
58
            if (array_key_exists($key, $condition)) {
59
                $this->{"where$key"}($condition[$key]);
60
                unset($condition[$key]);
61
            }
62
        }
63
        if (!empty($condition)) {
64
            parent::andWhere($condition, $params);
65
        }
66
67
        return $this;
68
    }
69
70
    public function whereId($id)
71
    {
72
        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...
73
    }
74
75
    public function whereEmail($username)
76
    {
77
        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 whereEmail()). 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...
78
    }
79
80
    public function whereUsername($username)
81
    {
82
        $userId = (int)$username;
83
        if ($userId > 0 && "$userId" === trim($username)) {
84
            return $this->whereId($userId);
85
        }
86
87
        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...
88
    }
89
90
    public function wherePassword($password)
91
    {
92
        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...
93
            'check_password(:password,c.password) OR check_password(:password,tmp.value)',
94
            [':password' => $password]
95
        )->leftJoin('value tmp', "tmp.obj_id=c.obj_id AND tmp.prop_id=prop_id('client,access:tmp_pwd')");
96
    }
97
98
    public function whereActive($is_active)
99
    {
100
        if (is_null($is_active)) {
101
            return $this;
102
        }
103
104
        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...
105
    }
106
}
107