UserAssociation::validateAttribute()   C
last analyzed

Complexity

Conditions 7
Paths 19

Size

Total Lines 51
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 0
cts 32
cp 0
rs 6.9743
c 0
b 0
f 0
cc 7
eloc 22
nc 19
nop 2
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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