Completed
Push — master ( eb0b7d...7ea6d5 )
by Andrii
11:32
created

ClientQuery   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 73
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 24 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
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
                'k.first_name   AS first_name',
28
                'k.last_name    AS last_name',
29
                'i.value        AS allowed_ips',
30
                'coalesce(c.email,k.email) AS email',
31
            ])
32
            ->from('client          c')
33
            ->innerJoin('client     r', 'r.obj_id=c.seller_id')
34
            ->innerJoin('ref        y', 'y.obj_id=c.type_id')
35
            ->innerJoin('ref        z', "z.obj_id=c.state_id AND z.name IN ('ok')")
36
            ->leftJoin('contact     k', 'k.obj_id=c.obj_id')
37
            ->leftJoin('value       i', "i.obj_id=c.obj_id AND i.prop_id=prop_id('client,access:allowed_ips')")
38
        ;
39
    }
40
41
    public function andWhere($condition)
42
    {
43
        if (!is_array($condition) || $condition[0]) {
44
            return parent::andWhere($condition);
45
        }
46
        foreach (['id', 'username', 'password', 'email'] as $key) {
47
            if (isset($condition[$key])) {
48
                $this->{"where$key"}($condition[$key]);
49
                unset($condition[$key]);
50
            }
51
        }
52
        if (!empty($condition)) {
53
            $this->andWhere($condition);
54
        }
55
56
        return $this;
57
    }
58
59
    public function whereId($id)
60
    {
61
        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...
62
    }
63
64
    public function whereEmail($username)
65
    {
66
        return $this->whereUsername($username);
67
    }
68
69
    public function whereUsername($username)
70
    {
71
        $userId = (int) $username;
72
        if ($userId > 0) {
73
            return $this->whereId($userId);
74
        }
75
76
        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...
77
    }
78
79
    public function wherePassword($password)
80
    {
81
        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...
82
            'check_password(:password,c.password) OR check_password(:password,t.value)',
83
            [':password' => $password]
84
        )->leftJoin('value t', "t.obj_id=c.obj_id AND t.prop_id=prop_id('client,access:tmp_pwd')");
85
    }
86
}
87