Completed
Push — master ( 829e7b...9495f2 )
by Nate
01:24
created

UsersAttributeTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 12
dl 0
loc 120
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserManager() 0 8 2
A eagerLoadingUsersMap() 0 16 1
A setUsersFromRequest() 0 8 2
A userQuery() 0 19 2
A getUsers() 0 13 1
A setUsers() 0 5 1
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\managers\RelationshipManagerInterface;
18
use flipbox\organizations\managers\UserRelationshipManager;
19
use flipbox\organizations\records\UserAssociation;
20
use Tightenco\Collect\Support\Collection;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 *
26
 * @mixin Organization
27
 */
28
trait UsersAttributeTrait
29
{
30
    /**
31
     * @var RelationshipManagerInterface
32
     */
33
    private $userManager;
34
35
    /**
36
     * @return RelationshipManagerInterface
37
     */
38
    public function getUserManager(): RelationshipManagerInterface
39
    {
40
        if (null === $this->userManager) {
41
            $this->userManager = new UserRelationshipManager($this);
42
        }
43
44
        return $this->userManager;
45
    }
46
47
    /**
48
     * @param array $sourceElements
49
     * @return array
50
     */
51
    private static function eagerLoadingUsersMap(array $sourceElements)
52
    {
53
        // Get the source element IDs
54
        $sourceElementIds = ArrayHelper::getColumn($sourceElements, 'id');
55
56
        $map = (new Query())
57
            ->select(['organizationId as source', 'userId as target'])
58
            ->from(UserAssociation::tableName())
59
            ->where(['organizationId' => $sourceElementIds])
60
            ->all();
61
62
        return [
63
            'elementType' => User::class,
64
            'map' => $map
65
        ];
66
    }
67
68
    /************************************************************
69
     * REQUEST
70
     ************************************************************/
71
72
    /**
73
     * AssociateUserToOrganization an array of users from request input
74
     *
75
     * @param string $identifier
76
     * @return $this
77
     */
78
    public function setUsersFromRequest(string $identifier = 'users')
79
    {
80
        if (null !== ($users = Craft::$app->getRequest()->getBodyParam($identifier))) {
81
            $this->getUserManager()->setMany((array)$users);
82
        }
83
84
        return $this;
85
    }
86
87
88
    /************************************************************
89
     * USERS QUERY
90
     ************************************************************/
91
92
    /**
93
     * @param array $criteria
94
     * @return UserQuery
95
     */
96
    public function userQuery($criteria = []): UserQuery
97
    {
98
        /** @noinspection PhpUndefinedMethodInspection */
99
        $query = User::find()
100
            ->organization($this)
101
            ->orderBy([
102
                'userOrder' => SORT_ASC,
103
                'username' => SORT_ASC,
104
            ]);
105
106
        if (!empty($criteria)) {
107
            QueryHelper::configure(
108
                $query,
109
                $criteria
110
            );
111
        }
112
113
        return $query;
114
    }
115
116
    /**
117
     * Get an array of users associated to an organization
118
     *
119
     * @param array $criteria
120
     * @return User[]|Collection
121
     */
122
    public function getUsers(array $criteria = []): Collection
123
    {
124
        return Collection::make(QueryHelper::configure(
125
            $this->userQuery($criteria)
126
                ->id(
127
                    $this->getUserManager()->findAll()
0 ignored issues
show
Documentation introduced by
$this->getUserManager()-...dAll()->pluck('userId') is of type object<Tightenco\Collect\Support\Collection>, but the function expects a integer|array<integer,integer>|false|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128
                        ->pluck('userId')
129
                )
130
                ->fixedOrder(true)
131
                ->limit(null),
132
            $criteria
133
        )->all());
134
    }
135
136
    /**
137
     * Set an array or query of users to an organization
138
     *
139
     * @param $users
140
     * @return $this
141
     */
142
    public function setUsers($users)
143
    {
144
        $this->getUserManager()->setMany($users);
145
        return $this;
146
    }
147
}
148