Completed
Push — develop ( e5a38c...1477af )
by Nate
02:53
created

TransitionUserState::transitionState()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 51
cp 0
rs 9.0036
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 20

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/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\organizations\events\handlers;
10
11
use craft\elements\User;
12
use craft\events\ModelEvent;
13
use flipbox\organizations\Organizations;
14
use flipbox\organizations\records\UserAssociation;
15
use yii\helpers\Json;
16
use yii\web\UserEvent;
17
18
/**
19
 * This event will transition a user from 'invited' to another ('active' by default) upon login.
20
 *
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 3.0.0
23
 */
24
class TransitionUserState
25
{
26
    /**
27
     * The state to transition to.
28
     *
29
     * @var string
30
     */
31
    public static $state = UserAssociation::STATE_ACTIVE;
32
33
    /**
34
     * @param ModelEvent $event
35
     */
36
    public static function handle(UserEvent $event)
37
    {
38
        /** @var User $user */
39
        $user = $event->identity;
40
41
        try {
42
            static::transitionStates($user);
43
        } catch (\Exception $e) {
44
            Organizations::warning(
45
                sprintf(
46
                    "Exception caught while trying to transition user '%s' organization type states. Exception: [%s].",
47
                    $user->getId(),
48
                    (string)Json::encode([
49
                        'Trace' => $e->getTraceAsString(),
50
                        'File' => $e->getFile(),
51
                        'Line' => $e->getLine(),
52
                        'Code' => $e->getCode(),
53
                        'Message' => $e->getMessage()
54
                    ])
55
                ),
56
                __METHOD__
57
            );
58
        }
59
    }
60
61
    /**
62
     * @param User $user
63
     */
64
    protected static function transitionStates(User $user)
65
    {
66
        $userAssociations = $user->getOrganizations()
67
            ->getRelationships()
68
            ->where('state', UserAssociation::STATE_INVITED);
69
70
        /** @var UserAssociation $association */
71
        foreach ($userAssociations as $association) {
72
            static::transitionState($association);
73
        }
74
    }
75
76
    /**
77
     * @param UserAssociation $association
78
     */
79
    protected static function transitionState(UserAssociation $association)
80
    {
81
        if ($association->state == static::$state) {
82
            return;
83
        }
84
85
        $state = $association->state;
86
        $association->state = static::$state;
87
88
        Organizations::info(
89
            sprintf(
90
                "Transitioning user '%s' associated to organization '%s' from state '%s' to '%s'.",
91
                $association->getUserId(),
92
                $association->getOrganizationId(),
93
                $state,
94
                $association->state
95
            ),
96
            __METHOD__
97
        );
98
99
        try {
100
            if (!$association->save(true)) {
101
                Organizations::warning(
102
                    sprintf(
103
                        "Failed to transition user '%s' associated to organization '%s' from state '%s' to '%s'. Exception: [%s].",
104
                        $association->getUserId(),
105
                        $association->getOrganizationId(),
106
                        $state,
107
                        $association->state,
108
                        (string)Json::encode($association->getErrors())
109
                    ),
110
                    __METHOD__
111
                );
112
            };
113
        } catch (\Exception $e) {
114
            Organizations::warning(
115
                sprintf(
116
                    "Exception caught while trying to transition user '%s' associated to organization '%s' from state '%s' to '%s'. Exception: [%s].",
117
                    $association->getUserId(),
118
                    $association->getOrganizationId(),
119
                    $state,
120
                    $association->state,
121
                    (string)Json::encode([
122
                        'Trace' => $e->getTraceAsString(),
123
                        'File' => $e->getFile(),
124
                        'Line' => $e->getLine(),
125
                        'Code' => $e->getCode(),
126
                        'Message' => $e->getMessage()
127
                    ])
128
                ),
129
                __METHOD__
130
            );
131
        }
132
    }
133
}
134