Completed
Push — master ( 5909bf...4f0a67 )
by Andrii
06:13
created

Client   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 97
Duplicated Lines 8.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 8
loc 97
ccs 0
cts 73
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 8 8 1
A init() 0 8 1
A onBeforeInsert() 0 7 2
A onBeforeSave() 0 15 4
A onAfterSave() 0 11 3
A saveValue() 0 8 1
A find() 0 4 1
A setId() 0 4 1
A getId() 0 4 1
A getSeller_id() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
use Yii;
15
use yii\db\Expression;
16
17
/**
18
 * Client model.
19
 *
20
 * @property integer $obj_id PK
21
 * @property integer $id synced with obj_id
22
 * @property integer $seller_id
23
 * @property string $password
24
 * @property string $email
25
 */
26
class Client extends \yii\db\ActiveRecord
27
{
28
    public $type;
29
    public $state;
30
    public $roles;
31
    public $seller;
32
    public $username;
33
    public $last_name;
34
    public $first_name;
35
36
    public $email_confirmed;
37
    public $allowed_ips;
38
    public $totp_secret;
39
40 View Code Duplication
    public function rules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        return [
43
            [['username', 'email', 'password', 'first_name', 'last_name'], 'trim'],
44
            [['state'], 'trim'],
45
            [['email_confirmed', 'allowed_ips', 'totp_secret'], 'trim'],
46
        ];
47
    }
48
49
    public function init()
50
    {
51
        parent::init();
52
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
53
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onBeforeSave']);
54
        $this->on(static::EVENT_AFTER_INSERT,  [$this, 'onAfterSave']);
55
        $this->on(static::EVENT_AFTER_UPDATE,  [$this, 'onAfterSave']);
56
    }
57
58
    public function onBeforeInsert()
59
    {
60
        $seller = static::findOne(['username' => Yii::$app->params['user.seller']]);
61
        $this->login = $this->username ?: $this->email;
62
        $this->seller_id = $seller->id;
63
        $this->onBeforeSave();
64
    }
65
66
    public function onBeforeSave()
67
    {
68
        if (!empty($this->state)) {
69
            $this->state_id = new Expression($this->state==='ok'
70
                ? "coalesce(state_id('client,ok'),state_id('client,active'))"
71
                : "state_id('client,{$this->state}')"
72
            );
73
        }
74
        if ($this->email_confirmed) {
75
            $this->email = $this->email_confirmed;
76
            $this->saveValue('contact:email_new', '');
77
            $this->saveValue('contact:email_confirmed', $this->email_confirmed);
78
            $this->saveValue('contact:email_confirm_date', new Expression('now()::text'));
79
        }
80
    }
81
82
    public function onAfterSave()
83
    {
84
        $this->id = $this->id ?: $this->obj_id;
85
        $this->type = $this->type ?: 'client';
86
        $contact = Contact::findOne($this->id);
87
        $contact->setAttributes($this->getAttributes($contact->safeAttributes()));
88
        $contact->save();
89
        $this->saveValue('client,access:totp_secret', $this->totp_secret);
90
        $this->saveValue('client,access:allowed_ips', $this->allowed_ips);
91
        $this->saveValue('login_ips:panel', $this->allowed_ips);
92
    }
93
94
    public function saveValue($prop, $value)
95
    {
96
        self::getDb()->createCommand('SELECT set_value(:id,:prop,:value)', [
97
            'id' => $this->id,
98
            'prop' => $prop,
99
            'value' => $value,
100
        ])->execute();
101
    }
102
103
    public static function find()
104
    {
105
        return new ClientQuery(get_called_class());
106
    }
107
108
    public function setId($value)
109
    {
110
        $this->obj_id = $value;
111
    }
112
113
    public function getId()
114
    {
115
        return $this->obj_id;
116
    }
117
118
    public function getSeller_id()
119
    {
120
        return $this->reseller_id;
121
    }
122
}
123