Completed
Pull Request — master (#12)
by
unknown
14:18
created

Client::saveReferralParams()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 12
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\base\InvalidConfigException;
15
use yii\db\Exception;
16
use yii\db\Expression;
17
use yii\helpers\Json;
18
19
/**
20
 * Client model.
21
 *
22
 * @property integer $obj_id PK
23
 * @property integer $id synced with obj_id
24
 * @property integer $seller_id
25
 * @property string $password
26
 * @property string $email
27
 */
28
class Client extends \yii\db\ActiveRecord
29
{
30
    public $type;
31
    public $state;
32
    public $roles;
33
    public $seller;
34
    public $username;
35
    public $last_name;
36
    public $first_name;
37
    public $send_me_news;
38
39
    public $email_confirmed;
40
    public $email_new;
41
    public $allowed_ips;
42
    public $totp_secret;
43
    public $referralParams;
44
45
    public $password_hash;
46
47
    public static function tableName()
48
    {
49
        return '{{zclient}}';
50
    }
51
52
    public static function primaryKey()
53
    {
54
        return ['obj_id'];
55
    }
56
57
    public function rules()
58
    {
59
        return [
60
            [['username', 'email', 'password', 'first_name', 'last_name', 'email_new'], 'trim'],
61
            [['username', 'email'], 'filter', 'filter' => 'strtolower'],
62
            [['seller_id'], 'integer'],
63
            [['state'], 'trim'],
64
            [['email_confirmed', 'allowed_ips', 'totp_secret'], 'trim'],
65
            ['send_me_news', 'boolean'],
66
            ['referralParams', 'safe']
67
        ];
68
    }
69
70
    public function init()
71
    {
72
        parent::init();
73
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
74
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onBeforeSave']);
75
        $this->on(static::EVENT_AFTER_INSERT,  [$this, 'onAfterSave']);
76
        $this->on(static::EVENT_AFTER_UPDATE,  [$this, 'onAfterSave']);
77
    }
78
79
    public function onBeforeInsert()
80
    {
81
        $seller = static::findOne(['username' => Yii::$app->params['user.seller']]);
82
        $this->login = $this->username ?: $this->email;
83
        $this->seller_id = $seller->id;
84
        $this->onBeforeSave();
85
    }
86
87
    public function onBeforeSave()
88
    {
89
        if (empty($this->password)) {
90
            unset($this->password);
91
        }
92
        if (!empty($this->state)) {
93
            $this->state_id = new Expression("zref_id('state,client,{$this->state}')");
94
        }
95
96
        if ($this->isEmailConfirmAction()) {
97
            $double = static::findOne(['email' => $this->email_confirmed]);
98
            if (empty($double) || $this->obj_id === $double->obj_id) {
99
                $this->email = $this->email_confirmed;
100
            }
101
            $this->saveValue('contact:email_new', '');
102
            $this->saveValue('contact:email_confirmed', $this->email_confirmed);
103
            $this->saveValue('contact:email_confirm_date', new Expression("date_trunc('second', now()::timestamp)::text"));
104
        }
105
        if (!empty($this->email_new)) {
106
            $this->saveValue('contact:email_new', $this->email_new);
107
        }
108
    }
109
110
    private function isEmailConfirmAction(): bool
111
    {
112
        $currentConfirmedEmail = $this->readValue('contact:email_confirmed');
113
114
        return !empty($this->email_confirmed) && ($currentConfirmedEmail !== $this->email_confirmed);
115
    }
116
117
    public function onAfterSave()
118
    {
119
        $this->id = $this->id ?: $this->getAgain()->id;
120
        $this->type = $this->type ?: $this->getAgain()->type;
121
        $send_news = $this->send_me_news === '0' ? '' : 1;
122
123
        $contact = Contact::findOne($this->id);
124
        $contact->setAttributes($this->getAttributes($contact->safeAttributes()));
125
        $contact->save();
126
        $this->saveValue('client,access:totp_secret', $this->totp_secret);
127
        $this->saveValue('client,access:allowed_ips', $this->allowed_ips);
128
        $this->saveValue('login_ips:panel', $this->allowed_ips);
129
130
        $this->saveValue('contact:policy_consent', 1);
131
        $this->saveValue('contact:gdpr_consent', 1);
132
        $this->saveValue('client,mailing:commercial', $send_news);
133
        $this->saveValue('client,mailing:newsletters', $send_news);
134
135
        $this->saveReferralParams();
136
    }
137
138
    private function saveReferralParams(): void
139
    {
140
        if (!empty($this->referralParams['referer'])) {
141
            $this->saveValue("client,registration:referer", $this->referralParams['referer']);
142
        }
143
        if (!empty($this->referralParams['utmTags'])) {
144
            $this->saveValue("client,registration:utm_tags", Json::htmlEncode($this->referralParams['utm_tags']));
145
        }
146
    }
147
148
    protected $_again;
149
150
    public function getAgain()
151
    {
152
        /// XXX this crutch is needed bacause we use `zclient` view (not table)
153
        /// XXX and yii ActiveRecord doesn't populate model properly in this case
154
        if ($this->_again === null) {
155
            $this->_again = static::find()->whereUsername($this->username)->one();
156
        }
157
158
        return $this->_again;
159
    }
160
161
    private function readValue(string $prop): string
162
    {
163
        $params = [
164
            'id' => $this->id,
165
            'prop' => $prop,
166
        ];
167
        return self::getDb()->createCommand("SELECT get_value(:id,:prop)", $params)->queryScalar();
168
    }
169
170
    public function saveValue($prop, $value)
171
    {
172
        $params = [
173
            'id' => $this->id,
174
            'prop' => $prop,
175
            'value' => $value,
176
        ];
177
        $sub = ':value';
178
        if ($value instanceof Expression) {
0 ignored issues
show
Bug introduced by
The class yii\db\Expression does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
179
            $sub = (string)$value;
180
            unset($params['value']);
181
        }
182
        self::getDb()->createCommand("SELECT set_value(:id,:prop,$sub)", $params)->execute();
183
    }
184
185
    public static function find()
186
    {
187
        return new ClientQuery(get_called_class());
188
    }
189
190
    public function setId($value)
191
    {
192
        $this->obj_id = $value;
193
    }
194
195
    public function getId()
196
    {
197
        return $this->obj_id;
198
    }
199
200
    public function getSeller_id()
201
    {
202
        return $this->reseller_id;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function getPasswordHash()
209
    {
210
        return $this->password_hash;
211
    }
212
213
    public function getPassword_hash()
214
    {
215
        return $this->getAuthKey();
216
    }
217
218
    /**
219
     * @param string $email
220
     * @return bool
221
     */
222
    public function updateEmail(string $email): bool
223
    {
224
        if ($this->username) {
225
            try {
226
                if (Yii::$app->db->createCommand()
227
                    ->update('zclient', ['email' => $email], 'login = :login')
228
                    ->bindValue(':login', $this->username)
229
                    ->execute()) {
230
                    return true;
231
                }
232
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class yii\db\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
233
            }
234
        }
235
236
        return false;
237
    }
238
239
    protected static function filterCondition(array $condition, array $aliases = [])
240
    {
241
        /// XXX skip condition filtering
242
        return $condition;
243
    }
244
}
245