UserAssociation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 67
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C validateAttribute() 0 51 7
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\validators;
10
11
use Craft;
12
use flipbox\organization\Organization as OrganizationPlugin;
13
use flipbox\organization\records\Organization;
14
use yii\validators\Validator;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class UserAssociation extends Validator
21
{
22
23
    /**
24
     * @var boolean whether this validation rule should be skipped if the attribute value
25
     * is null or an empty string.
26
     */
27
    public $skipOnEmpty = true;
28
29
    /**
30
     * @param Organization $object
31
     * @param $attribute
32
     *
33
     * @return void
34
     */
35
    public function validateAttribute($object, $attribute)
36
    {
37
38
        $value = $object->{$attribute};
39
40
        // Do we have a value?
41
        if (!empty($value)) {
42
            // If a user doesn't exist, than this means we have an invalid owner element
43
            if (!$userModel = Craft::$app->getUsers()->getUserById($value)) {
44
                // Invalid status message
45
                $message = Craft::t('organization', 'User is of invalid type.');
46
47
                // Add error
48
                $this->addError($object, $attribute, $message);
49
            }
50
51
            // Are they already a member of another organization?
52
            if (OrganizationPlugin::getInstance()->getSettings()->userAssociationRestriction()) {
53
                $query = OrganizationPlugin::getInstance()->getOrganization()->getQuery([
54
                    'id' => 'not ' . $object->id,
55
                    'status' => null,
56
                    'user' => $userModel->id
57
                ]);
58
59
                if ($query->count()) {
60
                    // Invalid status message
61
                    $message = Craft::t('organization', 'Owner is a member of another organization.');
62
63
                    // Add error
64
                    $this->addError($object, $attribute, $message);
65
                }
66
            }
67
68
            // Are they already a member of another organization?
69
            if (OrganizationPlugin::getInstance()->getSettings()->memberAssociationRestriction()) {
70
                $query = OrganizationPlugin::getInstance()->getOrganization()->getQuery([
71
                    'id' => 'not ' . $object->id,
72
                    'status' => null,
73
                    'member' => $userModel->id
74
                ]);
75
76
                if ($query->count()) {
77
                    // Invalid status message
78
                    $message = Craft::t('organization', 'Owner is a member of another organization.');
79
80
                    // Add error
81
                    $this->addError($object, $attribute, $message);
82
                }
83
            }
84
        }
85
    }
86
}
87