UsersAttributeTrait::setUsers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\organizations\elements;
10
11
use Craft;
12
use craft\db\Query;
13
use craft\elements\db\UserQuery;
14
use craft\elements\User;
15
use craft\helpers\ArrayHelper;
16
use flipbox\craft\ember\helpers\QueryHelper;
17
use flipbox\organizations\relationships\OrganizationTypeRelationship;
18
use flipbox\organizations\relationships\RelationshipInterface;
19
use flipbox\organizations\relationships\UserRelationship;
20
use flipbox\organizations\records\UserAssociation;
21
use Tightenco\Collect\Support\Collection;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 *
27
 * @mixin Organization
28
 */
29
trait UsersAttributeTrait
30
{
31
    /**
32
     * @var RelationshipInterface
33
     */
34
    private $userManager;
35
36
    /**
37
     * @param array $sourceElements
38
     * @return array
39
     */
40
    private static function eagerLoadingUsersMap(array $sourceElements)
41
    {
42
        // Get the source element IDs
43
        $sourceElementIds = ArrayHelper::getColumn($sourceElements, 'id');
44
45
        $map = (new Query())
46
            ->select(['organizationId as source', 'userId as target'])
47
            ->from(UserAssociation::tableName())
48
            ->where(['organizationId' => $sourceElementIds])
49
            ->all();
50
51
        return [
52
            'elementType' => User::class,
53
            'map' => $map
54
        ];
55
    }
56
57
58
    /************************************************************
59
     * REQUEST
60
     ************************************************************/
61
62
    /**
63
     * AssociateUserToOrganization an array of users from request input
64
     *
65
     * @param string $identifier
66
     * @return $this
67
     */
68
    public function setUsersFromRequest(string $identifier = 'users')
69
    {
70
        if (null !== ($users = Craft::$app->getRequest()->getBodyParam($identifier))) {
71
            $this->getUsers()->clear()->add($users);
72
        }
73
74
        return $this;
75
    }
76
77
78
    /************************************************************
79
     * USERS
80
     ************************************************************/
81
82
    /**
83
     * Get an array of users associated to an organization
84
     *
85
     * @return UserRelationship|RelationshipInterface
86
     */
87
    public function getUsers(): RelationshipInterface
88
    {
89
        if (null === $this->userManager) {
90
            $this->userManager = new UserRelationship($this);
91
        }
92
93
        return $this->userManager;
94
    }
95
96
    /**
97
     * Set an array or query of users to an organization
98
     *
99
     * @param $users
100
     * @return $this
101
     */
102
    public function setUsers($users)
103
    {
104
        $this->getUsers()->clear()->add($users);
105
        return $this;
106
    }
107
}
108