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

Client   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 53
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 7 1
A init() 0 7 1
A onBeforeInsert() 0 6 2
A onAfterSave() 0 13 3
A find() 0 4 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
use Yii;
15
16
/**
17
 * Client model.
18
 *
19
 * @property integer $obj_id PK
20
 * @property integer $seller_id
21
 * @property string $password
22
 * @property string $email
23
 */
24
class Client extends \yii\db\ActiveRecord
25
{
26
    public $id;
27
    public $type;
28
    public $state;
29
    public $seller;
30
    public $username;
31
    public $last_name;
32
    public $first_name;
33
    public $allowed_ips;
34
35
    public function rules()
36
    {
37
        return [
38
            [['username', 'email', 'password', 'first_name', 'last_name'], 'trim'],
39
            ['allowed_ips', 'trim'],
40
        ];
41
    }
42
43
    public function init()
44
    {
45
        parent::init();
46
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
47
        $this->on(static::EVENT_AFTER_INSERT,  [$this, 'onAfterSave']);
48
        $this->on(static::EVENT_AFTER_UPDATE,  [$this, 'onAfterSave']);
49
    }
50
51
    public function onBeforeInsert()
52
    {
53
        $seller = static::findOne(['username' => Yii::$app->params['user.seller']]);
54
        $this->login = $this->username ?: $this->email;
55
        $this->seller_id = $seller->id;
56
    }
57
58
    public function onAfterSave()
59
    {
60
        $this->id = $this->id ?: $this->obj_id;
61
        $this->type = $this->type ?: 'client';
62
        $contact = Contact::findOne($this->id);
63
        $contact->setAttributes($this->getAttributes());
64
        $contact->save();
65
        self::getDb()->createCommand('SELECT set_value(:id,:prop,:value)', [
66
            'id' => $this->id,
67
            'prop' => 'client,access:allowed_ips',
68
            'value' => $this->allowed_ips,
69
        ])->execute();
70
    }
71
72
    public static function find()
73
    {
74
        return new ClientQuery(get_called_class());
75
    }
76
}
77