Completed
Push — master ( 663af3...8c89d0 )
by vistart
20:53
created

Module::getOrganizationByEntrance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization\web\organization;
14
15
use rhosocial\organization\Organization;
16
use Yii;
17
use yii\base\InvalidParamException;
18
19
/**
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
class Module extends \yii\base\Module
24
{
25
    const RESULT_SUCCESS = 'success';
26
    const RESULT_FAILED = 'failed';
27
    const SESSION_KEY_MESSAGE = 'session_key_message';
28
    const SESSION_KEY_RESULT = 'session_key_result';
29
30
    /**
31
     * Get organization.
32
     * @param Organization|string|integer $organization
33
     * @return Organization
34
     */
35
    public static function getOrganization($organization)
36
    {
37
        if (!$organization) {
38
            return null;
39
        }
40
        $class = Yii::$app->user->identity->organizationClass;
41
        if ($organization instanceof $class) {
42
            $organization = $organization->getID();
43
        }
44
        return $class::find()->guidOrId($organization)->one();
45
    }
46
47
    /**
48
     * @param string $entrance
49
     * @return Organization
50
     */
51
    public static function getOrganizationByEntrance($entrance)
52
    {
53
        $entrance = (string)$entrance;
54
        if ($entrance === '') {
55
            throw new InvalidParamException(Yii::t('organization', "Entrance should not be empty."));
56
        }
57
        $class = Yii::$app->user->identity->organizationClass;
58
        $noInit = $class::buildNoInitModel();
59
        /* @var $noInit Organization */
60
        $settingClass = $noInit->organizationSettingClass;
61
        $setting = $settingClass::find()->andWhere([
62
            $noInit->getNoInitOrganizationSetting()->idAttribute => $class::SETTING_ITEM_JOIN_ENTRANCE_URL,
63
            $noInit->getNoInitOrganizationSetting()->contentAttribute => $entrance,
64
        ])->one();
65
        if (!$setting) {
66
            return null;
67
        }
68
        return $setting->host;
69
    }
70
}
71