Completed
Push — master ( fef881...1aed96 )
by vistart
20:35
created

JoinController::initMessages()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 7.756
cc 9
eloc 9
nc 16
nop 0
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
     * Initialize messages.
46
     */
47
    protected function initMessages()
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 init()
67
    {
68
        $this->initMessages();
69
        parent::init();
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function behaviors()
76
    {
77
        return [
78
            'access' => [
79
                'class' => AccessControl::class,
80
                'rules' => [
81
                    [
82
                        'allow' => false,
83
                        'roles' => ['?'],
84
                    ],
85
                    [
86
                        'allow' => true,
87
                        'roles' => ['@'],
88
                    ],
89
                ],
90
            ],
91
            'verbs' => [
92
                'class' => VerbFilter::class,
93
                'actions' => [
94
                    'join' => ['post'],
95
                    'exit' => ['post'],
96
                ]
97
            ],
98
        ];
99
    }
100
101
    /**
102
     * @param string $entrance
103
     * @return Organization
104
     * @throws BadRequestHttpException
105
     * @throws OrganizationNotFoundException
106
     */
107
    public static function getOrganization($entrance)
108
    {
109
        try {
110
            $organization = Module::getOrganizationByEntrance($entrance);
111
            if (!$organization) {
112
                throw new OrganizationNotFoundException();
113
            }
114
        } catch (InvalidParamException $ex) {
115
            throw new BadRequestHttpException($ex->getMessage());
116
        }
117
        return $organization;
118
    }
119
120
    /**
121
     * @param $entrance
122
     * @return Response|string
123
     */
124
    public function actionIndex($entrance)
125
    {
126
        $organization = static::getOrganization($entrance);
127
        $model = new JoinOrganizationForm(['organization' => $organization]);
128
        return $this->render('index', [
129
            'model' => $model,
130
        ]);
131
    }
132
133
    /**
134
     * @param string $entrance
135
     * @return Response|string
136
     * @throws UnauthorizedHttpException
137
     */
138
    public function actionJoin($entrance)
139
    {
140
        $organization = static::getOrganization($entrance);
141
        $user = Yii::$app->user->identity;
142
        if ($organization->creator->equals($user)) {
143
            return $this->redirect(['index', 'entrance' => $entrance]);
144
        }
145
        $model = new JoinOrganizationForm(['organization' => $organization]);
146
        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...
147
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
148
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinFailedMessage . ($model->hasErrors('password') ? ' ' . $model->getFirstError('password') : ''));
149
            return $this->redirect(['index', 'entrance' => $entrance]);
150
        }
151
        try {
152
            if ($organization->addMember($user)) {
153
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
154
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinSuccessMessage);
155
            } else {
156
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
157
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->joinFailedMessage);
158
            }
159
        } catch (\Exception $ex) {
160
            throw new UnauthorizedHttpException($ex->getMessage());
161
        }
162
        return $this->redirect(['index', 'entrance' => $entrance]);
163
    }
164
165
    /**
166
     * @param string $entrance
167
     * @return Response
168
     * @throws UnauthorizedHttpException
169
     */
170
    public function actionExit($entrance)
171
    {
172
        $organization = static::getOrganization($entrance);
173
        $user = Yii::$app->user->identity;
174
        if ($organization->creator->equals($user)) {
175
            return $this->redirect(['index', 'entrance' => $entrance]);
176
        }
177
        $model = new JoinOrganizationForm(['organization' => $organization]);
178
        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...
179
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
180
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitFailedMessage . ($model->hasErrors('password') ? ' ' . $model->getFirstError('password') : ''));
181
            return $this->redirect(['index', 'entrance' => $entrance]);
182
        }
183
        try {
184
            if ($organization->removeMember($user)) {
185
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
186
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitSuccessMessage);
187
            } else {
188
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
189
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitFailedMessage);
190
            }
191
        } catch (\Exception $ex) {
192
            throw new UnauthorizedHttpException($ex->getMessage());
193
        }
194
        return $this->redirect(['index', 'entrance' => $entrance]);
195
    }
196
}
197