RemoveUsers   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 76
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A settingsAttributes() 0 9 1
A isDestructive() 0 4 1
A getTriggerLabel() 0 4 1
B performAction() 0 35 5
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\organization\elements\actions;
10
11
use Craft;
12
use craft\base\ElementAction;
13
use craft\elements\db\ElementQueryInterface;
14
use craft\elements\User;
15
use flipbox\organization\elements\Organization;
16
use flipbox\organization\Organization as OrganizationPlugin;
17
use yii\base\Exception;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class RemoveUsers extends ElementAction
24
{
25
26
    /**
27
     * @var string|int|array|Organization
28
     */
29
    public $organization;
30
31
    /**
32
     * @return array
33
     */
34
    public function settingsAttributes(): array
35
    {
36
        return array_merge(
37
            parent::settingsAttributes(),
38
            [
39
                'organization'
40
            ]
41
        );
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public static function isDestructive(): bool
48
    {
49
        return true;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function getTriggerLabel(): string
56
    {
57
        return 'Remove';
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function performAction(ElementQueryInterface $query): bool
64
    {
65
66
        if (empty($this->organization)) {
67
            throw new Exception("Organization does not exist with the identifier “{$this->organization}”");
68
        }
69
70
        /** @var Organization $organization */
71
        $organization = OrganizationPlugin::getInstance()->getOrganization()->get($this->organization);
72
73
        /** @var User $user */
74
        foreach ($query->all() as $user) {
75
            if (!OrganizationPlugin::getInstance()->getUser()->dissociate($user, $organization)) {
76
                throw new Exception(
77
                    Craft::t(
78
                        'organization',
79
                        "Unable to disassociate user “{user}” from organization “{organization}”",
80
                        [
81
                            '{user}' => $user->getId(),
82
                            '{organization}' => $organization->getId()
83
                        ]
84
                    )
85
                );
86
            }
87
        }
88
89
        $this->setMessage(
90
            Craft::t(
91
                'organization',
92
                'User' . ($query->count() != 1 ? 's' : '') . ' removed.'
93
            )
94
        );
95
96
        return true;
97
    }
98
}
99