Completed
Push — master ( 090439...148aa2 )
by vistart
19:15
created

JoinController::actionExit()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 9
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
14
namespace rhosocial\organization\web\organization\controllers;
15
16
use rhosocial\organization\exceptions\OrganizationNotFoundException;
17
use rhosocial\organization\forms\JoinOrganizationForm;
18
use rhosocial\organization\Organization;
19
use rhosocial\organization\web\organization\Module;
20
use Yii;
21
use yii\base\InvalidParamException;
22
use yii\filters\AccessControl;
23
use yii\filters\VerbFilter;
24
use yii\web\BadRequestHttpException;
25
use yii\web\Controller;
26
use yii\web\Response;
27
use yii\web\UnauthorizedHttpException;
28
29
/**
30
 * Class JoinController
31
 * @package rhosocial\organization\web\organization\controllers
32
 * @version 1.0
33
 * @author vistart <[email protected]>
34
 */
35
class JoinController extends Controller
36
{
37
    public $layout = 'main';
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function behaviors()
43
    {
44
        return [
45
            'access' => [
46
                'class' => AccessControl::class,
47
                'rules' => [
48
                    [
49
                        'allow' => false,
50
                        'roles' => ['?'],
51
                    ],
52
                    [
53
                        'allow' => true,
54
                        'roles' => ['@'],
55
                    ],
56
                ],
57
            ],
58
            'verbs' => [
59
                'class' => VerbFilter::class,
60
                'actions' => [
61
                    'join' => ['post'],
62
                    'exit' => ['post'],
63
                ]
64
            ],
65
        ];
66
    }
67
68
    /**
69
     * @param string $entrance
70
     * @return Organization
71
     * @throws BadRequestHttpException
72
     * @throws OrganizationNotFoundException
73
     */
74
    public static function getOrganization($entrance)
75
    {
76
        try {
77
            $organization = Module::getOrganizationByEntrance($entrance);
78
            if (!$organization) {
79
                throw new OrganizationNotFoundException();
80
            }
81
        } catch (InvalidParamException $ex) {
82
            throw new BadRequestHttpException($ex->getMessage());
83
        }
84
        return $organization;
85
    }
86
87
    /**
88
     * @param $entrance
89
     * @return Response|string
90
     */
91
    public function actionIndex($entrance)
92
    {
93
        $organization = static::getOrganization($entrance);
94
        $model = new JoinOrganizationForm(['organization' => $organization]);
95
        return $this->render('index', [
96
            'model' => $model,
97
        ]);
98
    }
99
100
    /**
101
     * @param string $entrance
102
     * @return Response|string
103
     * @throws UnauthorizedHttpException
104
     */
105
    public function actionJoin($entrance)
106
    {
107
        $organization = static::getOrganization($entrance);
108
        $user = Yii::$app->user->identity;
109
        if ($organization->creator->equals($user)) {
110
            return $this->redirect(['index', 'entrance' => $entrance]);
111
        }
112
        $model = new JoinOrganizationForm(['organization' => $organization]);
113
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) && $model->validate()) {
114
            try {
115
                if ($organization->addMember($user)) {
116
                    Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
117
                    Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '');
118
                } else {
119
                    Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
120
                    Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '');
121
                }
122
            } catch (\Exception $ex) {
123
                throw new UnauthorizedHttpException($ex->getMessage());
124
            }
125
        }
126
        return $this->redirect(['index', 'entrance' => $entrance]);
127
    }
128
129
    /**
130
     * @param string $entrance
131
     * @return Response
132
     * @throws UnauthorizedHttpException
133
     */
134
    public function actionExit($entrance)
135
    {
136
        $organization = static::getOrganization($entrance);
137
        $user = Yii::$app->user->identity;
138
        if ($organization->creator->equals($user)) {
139
            return $this->redirect(['index', 'entrance' => $entrance]);
140
        }
141
        $model = new JoinOrganizationForm(['organization' => $organization]);
142
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) && $model->validate()) {
143
            try {
144
                if ($organization->removeMember($user)) {
145
                    Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
146
                    Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '');
147
                } else {
148
                    Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
149
                    Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '');
150
                }
151
            } catch (\Exception $ex) {
152
                throw new UnauthorizedHttpException($ex->getMessage());
153
            }
154
        }
155
        return $this->redirect(['index', 'entrance' => $entrance]);
156
    }
157
}
158