Completed
Push — master ( 94d0aa...c97419 )
by Andrii
13:33
created

SaveReferralParams::beforeSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Identity and Access Management server providing OAuth2, multi-factor authentication and more
4
 *
5
 * @link      https://github.com/hiqdev/hiam
6
 * @package   hiam
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiam\mrdp\behaviors;
12
13
use hiam\mrdp\models\Identity;
14
use Yii;
15
use yii\base\Application;
16
use yii\base\Event;
17
18
/**
19
 * SaveReturnUrl behavior.
20
 *
21
 * @package hiam\behaviors
22
 */
23
class SaveReferralParams extends \yii\base\Behavior
24
{
25
    /**
26
     * @inheritDoc
27
     */
28
    public function events(): array
29
    {
30
        return [
31
            Application::EVENT_BEFORE_REQUEST => 'beforeRequest',
32
            Identity::EVENT_BEFORE_SAVE => 'beforeSave',
33
        ];
34
    }
35
36
    /**
37
     * @param Event $event
38
     */
39
    public function beforeRequest(Event $event): void
40
    {
41
        $params = Yii::$app->request->getQueryParams();
42
        $utmTags = [];
43
        foreach ($params as $name => $value) {
44
            if (strstr($name, 'utm_')) {
45
                $utmTags[$name] = $value;
46
            }
47
        }
48
        $referalParams = array_filter([
49
            'referer' => $params['atid'],
50
            'utmTags' => $utmTags,
51
        ]);
52
        if (!empty($referalParams)) {
53
            Yii::$app->session->set('referralParams', $referalParams);
54
        }
55
    }
56
57
58
    public function beforeSave()
59
    {
60
        $this->owner->referralParams = \Yii::$app->session->get('referralParams');
61
    }
62
}
63