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

Client::onBeforeSave()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 0
crap 20
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