Completed
Push — master ( 6fe075...fef881 )
by vistart
17:36
created

JoinController::actionJoin()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
rs 5.3846
cc 8
eloc 20
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
    public $joinSuccessMessage;
40
    public $joinFailedMessage;
41
    public $exitSuccessMessage;
42
    public $exitFailedMessage;
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function init()
48
    {
49
        if (!is_string($this->joinSuccessMessage) || empty($this->joinSuccessMessage)) {
50
            $this->joinSuccessMessage = Yii::t('organization', 'Joined.');
51
        }
52
        if (!is_string($this->joinFailedMessage) || empty($this->joinFailedMessage)) {
53
            $this->joinFailedMessage = Yii::t('organization', 'Failed to join.');
54
        }
55
        if (!is_string($this->exitSuccessMessage) || empty($this->exitSuccessMessage)) {
56
            $this->exitSuccessMessage = Yii::t('organization', 'Exited.');
57
        }
58
        if (!is_string($this->exitFailedMessage) || empty($this->exitFailedMessage)) {
59
            $this->exitFailedMessage = Yii::t('organization', 'Failed to exit.');
60
        }
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function behaviors()
67
    {
68
        return [
69
            'access' => [
70
                'class' => AccessControl::class,
71
                'rules' => [
72
                    [
73
                        'allow' => false,
74
                        'roles' => ['?'],
75
                    ],
76
                    [
77
                        'allow' => true,
78
                        'roles' => ['@'],
79
                    ],
80
                ],
81
            ],
82
            'verbs' => [
83
                'class' => VerbFilter::class,
84
                'actions' => [
85
                    'join' => ['post'],
86
                    'exit' => ['post'],
87
                ]
88
            ],
89
        ];
90
    }
91
92
    /**
93
     * @param string $entrance
94
     * @return Organization
95
     * @throws BadRequestHttpException
96
     * @throws OrganizationNotFoundException
97
     */
98
    public static function getOrganization($entrance)
99
    {
100
        try {
101
            $organization = Module::getOrganizationByEntrance($entrance);
102
            if (!$organization) {
103
                throw new OrganizationNotFoundException();
104
            }
105
        } catch (InvalidParamException $ex) {
106
            throw new BadRequestHttpException($ex->getMessage());
107
        }
108
        return $organization;
109
    }
110
111
    /**
112
     * @param $entrance
113
     * @return Response|string
114
     */
115
    public function actionIndex($entrance)
116
    {
117
        $organization = static::getOrganization($entrance);
118
        $model = new JoinOrganizationForm(['organization' => $organization]);
119
        return $this->render('index', [
120
            'model' => $model,
121
        ]);
122
    }
123
124
    /**
125
     * @param string $entrance
126
     * @return Response|string
127
     * @throws UnauthorizedHttpException
128
     */
129
    public function actionJoin($entrance)
130
    {
131
        $organization = static::getOrganization($entrance);
132
        $user = Yii::$app->user->identity;
133
        if ($organization->creator->equals($user)) {
134
            return $this->redirect(['index', 'entrance' => $entrance]);
135
        }
136
        $model = new JoinOrganizationForm(['organization' => $organization]);
137
        if (!empty($organization->joinPassword) && (!$model->load(Yii::$app->request->post()) || !$model->validate('password'))) {
0 ignored issues
show
Documentation introduced by
'password' is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
139
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinFailedMessage . ($model->hasErrors('password') ? ' ' . $model->getFirstError('password') : ''));
140
            return $this->redirect(['index', 'entrance' => $entrance]);
141
        }
142
        try {
143
            if ($organization->addMember($user)) {
144
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
145
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinSuccessMessage);
146
            } else {
147
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
148
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinFailedMessage);
149
            }
150
        } catch (\Exception $ex) {
151
            throw new UnauthorizedHttpException($ex->getMessage());
152
        }
153
        return $this->redirect(['index', 'entrance' => $entrance]);
154
    }
155
156
    /**
157
     * @param string $entrance
158
     * @return Response
159
     * @throws UnauthorizedHttpException
160
     */
161
    public function actionExit($entrance)
162
    {
163
        $organization = static::getOrganization($entrance);
164
        $user = Yii::$app->user->identity;
165
        if ($organization->creator->equals($user)) {
166
            return $this->redirect(['index', 'entrance' => $entrance]);
167
        }
168
        $model = new JoinOrganizationForm(['organization' => $organization]);
169
        if (!empty($organization->joinPassword) && (!$model->load(Yii::$app->request->post()) || !$model->validate('password'))) {
0 ignored issues
show
Documentation introduced by
'password' is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
170
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
171
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitFailedMessage . ($model->hasErrors('password') ? ' ' . $model->getFirstError('password') : ''));
172
            return $this->redirect(['index', 'entrance' => $entrance]);
173
        }
174
        try {
175
            if ($organization->removeMember($user)) {
176
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
177
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitSuccessMessage);
178
            } else {
179
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
180
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitFailedMessage);
181
            }
182
        } catch (\Exception $ex) {
183
            throw new UnauthorizedHttpException($ex->getMessage());
184
        }
185
        return $this->redirect(['index', 'entrance' => $entrance]);
186
    }
187
}
188