Completed
Push — master ( 2731ff...16fe27 )
by Nate
03:36 queued 11s
created

handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 25
cp 0
rs 9.44
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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;
12
use craft\elements\User;
13
use craft\events\SetElementTableAttributeHtmlEvent;
14
use craft\helpers\Html;
15
use flipbox\organizations\Organizations;
16
use flipbox\organizations\records\UserAssociation;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class SetOrganizationUserElementTableAttributeHtml
23
{
24
    /**
25
     * @param SetElementTableAttributeHtmlEvent $event
26
     */
27
    public static function handle(SetElementTableAttributeHtmlEvent $event)
28
    {
29
        if ($event->attribute === 'state') {
30
            /** @var User $element */
31
            $element = $event->sender;
32
33
            $params = [
34
                'indicator' => '',
35
                'label' => 'N/A'
36
            ];
37
38
            $organizaiton = Craft::$app->getRequest()->getParam('organization');
39
            $state = Organizations::getInstance()->getSettings()->getUserStates();
40
41
            if ($association = UserAssociation::findOne([
42
                'user' => $element,
43
                'organization' => $organizaiton
44
            ])) {
45
                $params = [
46
                    'indicator' => $association->state,
47
                    'label' => $state[$association->state] ?? 'N/A'
48
                ];
49
            }
50
51
            $event->html = Html::encodeParams(
52
                '<span class="user-state status {indicator}"></span>{label}',
53
                $params
54
            );
55
        }
56
    }
57
}
58