Completed
Push — master ( 169348...67ce08 )
by Nate
01:48
created

UsersController::actionAssociate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 19
cp 0
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 2
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\cp\controllers;
10
11
use Craft;
12
use flipbox\ember\helpers\ArrayHelper;
13
use flipbox\organizations\actions\users\Associate;
14
use flipbox\organizations\actions\users\Dissociate;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class UsersController extends AbstractController
21
{
22
    /**
23
     * @return array
24
     */
25
    public function behaviors()
26
    {
27
        return ArrayHelper::merge(
28
            parent::behaviors(),
29
            [
30
                'error' => [
31
                    'default' => 'organization'
32
                ],
33
                'redirect' => [
34
                    'only' => ['associate', 'dissociate'],
35
                    'actions' => [
36
                        'associate' => [204],
37
                        'dissociate' => [204],
38
                    ]
39
                ],
40
                'flash' => [
41
                    'actions' => [
42
                        'associate' => [
43
                            204 => Craft::t('organizations', "Successfully associated user."),
44
                            401 => Craft::t('organizations', "Failed to associate user.")
45
                        ],
46
                        'dissociate' => [
47
                            204 => Craft::t('organizations', "Successfully dissociated user."),
48
                            401 => Craft::t('organizations', "Failed to dissociate user.")
49
                        ]
50
                    ]
51
                ]
52
            ]
53
        );
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    protected function verbs(): array
60
    {
61
        return [
62
            'associate' => ['post']
63
        ];
64
    }
65
66
    /**
67
     * @param int|string|null $user
68
     * @param int|string|null $organization
69
     * @return mixed
70
     * @throws \yii\base\InvalidConfigException
71
     */
72
    public function actionAssociate($user = null, $organization = null)
73
    {
74
        if (null === $organization) {
75
            $organization = Craft::$app->getRequest()->getBodyParam('organization');
76
        }
77
78
        if (null === $user) {
79
            $user = Craft::$app->getRequest()->getBodyParam('user');
80
        }
81
82
        /** @var Associate $action */
83
        $action = Craft::createObject([
84
            'class' => Associate::class
85
        ], [
86
            'associate',
87
            $this
88
        ]);
89
90
        return $action->runWithParams([
91
            'organization' => $organization,
92
            'user' => $user
93
        ]);
94
    }
95
96
    /**
97
     * @param int|string|null $user
98
     * @param int|string|null $organization
99
     * @return mixed
100
     * @throws \yii\base\InvalidConfigException
101
     */
102
    public function actionDissociate($user = null, $organization = null)
103
    {
104
        if (null === $organization) {
105
            $organization = Craft::$app->getRequest()->getBodyParam('organization');
106
        }
107
108
        if (null === $user) {
109
            $user = Craft::$app->getRequest()->getBodyParam('user');
110
        }
111
112
        /** @var Dissociate $action */
113
        $action = Craft::createObject([
114
            'class' => Dissociate::class
115
        ], [
116
            'dissociate',
117
            $this
118
        ]);
119
120
        return $action->runWithParams([
121
            'organization' => $organization,
122
            'user' => $user
123
        ]);
124
    }
125
}
126