Completed
Push — master ( 4f0a67...1cdf18 )
by Andrii
03:51
created

Client   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 1
dl 8
loc 100
ccs 0
cts 76
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
B onBeforeSave() 0 18 5
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
 * 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;
14
use yii\db\Expression;
15
16
/**
17
 * Client model.
18
 *
19
 * @property integer $obj_id PK
20
 * @property integer $id synced with obj_id
21
 * @property integer $seller_id
22
 * @property string $password
23
 * @property string $email
24
 */
25
class Client extends \yii\db\ActiveRecord
26
{
27
    public $type;
28
    public $state;
29
    public $roles;
30
    public $seller;
31
    public $username;
32
    public $last_name;
33
    public $first_name;
34
35
    public $email_confirmed;
36
    public $allowed_ips;
37
    public $totp_secret;
38
39 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...
40
    {
41
        return [
42
            [['username', 'email', 'password', 'first_name', 'last_name'], 'trim'],
43
            [['state'], 'trim'],
44
            [['email_confirmed', 'allowed_ips', 'totp_secret'], 'trim'],
45
        ];
46
    }
47
48
    public function init()
49
    {
50
        parent::init();
51
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
52
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onBeforeSave']);
53
        $this->on(static::EVENT_AFTER_INSERT,  [$this, 'onAfterSave']);
54
        $this->on(static::EVENT_AFTER_UPDATE,  [$this, 'onAfterSave']);
55
    }
56
57
    public function onBeforeInsert()
58
    {
59
        $seller = static::findOne(['username' => Yii::$app->params['user.seller']]);
60
        $this->login = $this->username ?: $this->email;
61
        $this->seller_id = $seller->id;
62
        $this->onBeforeSave();
63
    }
64
65
    public function onBeforeSave()
66
    {
67
        if (empty($this->password)) {
68
            unset($this->password);
69
        }
70
        if (!empty($this->state)) {
71
            $this->state_id = new Expression($this->state === 'ok'
72
                ? "coalesce(state_id('client,ok'),state_id('client,active'))"
73
                : "state_id('client,{$this->state}')"
74
            );
75
        }
76
        if ($this->email_confirmed) {
77
            $this->email = $this->email_confirmed;
78
            $this->saveValue('contact:email_new', '');
79
            $this->saveValue('contact:email_confirmed', $this->email_confirmed);
80
            $this->saveValue('contact:email_confirm_date', new Expression('now()::text'));
81
        }
82
    }
83
84
    public function onAfterSave()
85
    {
86
        $this->id = $this->id ?: $this->obj_id;
87
        $this->type = $this->type ?: 'client';
88
        $contact = Contact::findOne($this->id);
89
        $contact->setAttributes($this->getAttributes($contact->safeAttributes()));
90
        $contact->save();
91
        $this->saveValue('client,access:totp_secret', $this->totp_secret);
92
        $this->saveValue('client,access:allowed_ips', $this->allowed_ips);
93
        $this->saveValue('login_ips:panel', $this->allowed_ips);
94
    }
95
96
    public function saveValue($prop, $value)
97
    {
98
        self::getDb()->createCommand('SELECT set_value(:id,:prop,:value)', [
99
            'id' => $this->id,
100
            'prop' => $prop,
101
            'value' => $value,
102
        ])->execute();
103
    }
104
105
    public static function find()
106
    {
107
        return new ClientQuery(get_called_class());
108
    }
109
110
    public function setId($value)
111
    {
112
        $this->obj_id = $value;
113
    }
114
115
    public function getId()
116
    {
117
        return $this->obj_id;
118
    }
119
120
    public function getSeller_id()
121
    {
122
        return $this->reseller_id;
123
    }
124
}
125