Completed
Push — master ( 5980e7...b2c38b )
by Gaetano
05:32
created

UserMatcher::findUsersByGroup()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 0
cp 0
rs 9.552
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 20
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\User\User;
6
use Kaliop\eZMigrationBundle\API\Collection\UserCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9
class UserMatcher extends RepositoryMatcher implements KeyMatcherInterface
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_USER_ID = 'user_id';
14
    const MATCH_USER_LOGIN = 'login';
15
    const MATCH_USER_EMAIL = 'email';
16
    const MATCH_USERGROUP_ID = 'usergroup_id';
17
18
    protected $allowedConditions = array(
19
        self::MATCH_AND, self::MATCH_OR,
20
        self::MATCH_USER_ID, self::MATCH_USER_LOGIN, self::MATCH_USER_EMAIL,
21
        // aliases
22
        'id'
23
    );
24
    protected $returns = 'User';
25
26
    /**
27
     * @param array $conditions key: condition, value: int / string / int[] / string[]
28
     * @return UserCollection
29 3
     */
30
    public function match(array $conditions)
31 3
    {
32
        return $this->matchUser($conditions);
33
    }
34
35
    /**
36
     * @param array $conditions key: condition, value: int / string / int[] / string[]
37
     * @return UserCollection
38 3
     */
39
    public function matchUser(array $conditions)
40 3
    {
41
        $this->validateConditions($conditions);
42 3
43
        foreach ($conditions as $key => $values) {
44 3
45 3
            if (!is_array($values)) {
46
                $values = array($values);
47
            }
48
49 3
            switch ($key) {
50 3
                case 'id':
51 3
                case self::MATCH_USER_ID:
52
                   return new UserCollection($this->findUsersById($values));
53 3
54 2
                case self::MATCH_USER_LOGIN:
55
                    return new UserCollection($this->findUsersByLogin($values));
56 1
57 1
                case self::MATCH_USERGROUP_ID:
58
                    return new UserCollection($this->findUsersByGroup($values));
59
60
                case self::MATCH_USER_EMAIL:
61
                    return new UserCollection($this->findUsersByEmail($values));
62
63
                case self::MATCH_AND:
64
                    return $this->matchAnd($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchAnd($values); of type object|array adds the type array to the return on line 64 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...\UserMatcher::matchUser of type Kaliop\eZMigrationBundle...ion\UserCollection|null.
Loading history...
65
66
                case self::MATCH_OR:
67
                    return $this->matchOr($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchOr($values); of type object|array adds the type array to the return on line 67 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...\UserMatcher::matchUser of type Kaliop\eZMigrationBundle...ion\UserCollection|null.
Loading history...
68
            }
69
        }
70
    }
71
72
    /**
73 2
     * NB: bad luck if user login contains an email or otherwise @ character
74
     * @param string $key
75 2
     * @return array
76 2
     */
77
    protected function getConditionsFromKey($key)
78 2
    {
79
        if (is_int($key) || ctype_digit($key)) {
80
            return array(self::MATCH_USER_ID => $key);
81 2
        }
82
        if (strpos($key, '@') !== false) {
83
            return array(self::MATCH_USER_EMAIL => $key);
84
        }
85
        return array(self::MATCH_USER_LOGIN => $key);
86
    }
87
88 3
    /**
89
     * @param int[] $userIds
90 3
     * @return User[]
91
     */
92 3
    protected function findUsersById(array $userIds)
93
    {
94 3
        $users = [];
95
96 3
        foreach ($userIds as $userId) {
97
            // return unique contents
98
            $user = $this->repository->getUserService()->loadUser($userId);
99 3
100
            $users[$user->id] = $user;
101
        }
102
103
        return $users;
104
    }
105
106 2
    /**
107
     * @param string[] $logins
108 2
     * @return User[]
109
     */
110 2
    protected function findUsersByLogin(array $logins)
111
    {
112 2
        $users = [];
113
114 2
        foreach ($logins as $login) {
115
            // return unique contents
116
            $user = $this->repository->getUserService()->loadUserByLogin($login);
117 2
118
            $users[$user->id] = $user;
119
        }
120
121
        return $users;
122
    }
123
124
    /**
125
     * @param string[] $emails
126 1
     * @return User[]
127
     *
128 1
     * @todo check if this fails when user is not found
129
     */
130 1
    protected function findUsersByEmail(array $emails)
131
    {
132 1
        $users = [];
133
134 1
        foreach ($emails as $email) {
135 1
            // return unique contents
136
            $matches = $this->repository->getUserService()->loadUsersByEmail($email);
137
138
            foreach ($matches as $user) {
139 1
                $users[$user->id] = $user;
140
            }
141
        }
142
143
        return $users;
144
    }
145
146
    protected function findUsersByGroup(array $groupsIds)
147
    {
148
        $users = [];
149
150
        foreach ($groupsIds as $groupId) {
151
152
            $group = $this->repository->getUserService()->loadUserGroup($groupId);
153
154
            $offset = 0;
155
            $limit = 100;
156
            do {
157
                $matches = $this->repository->getUserService()->loadUsersOfUserGroup($group, $offset, $limit);
158
                $offset += $limit;
159
            } while (count($matches));
160
161
            // return unique contents
162
            foreach ($matches as $user) {
163
                $users[$user->id] = $user;
164
            }
165
        }
166
167
        return $users;
168
    }
169
}
170