Completed
Push — master ( 8c5037...eed80c )
by Gaetano
07:22
created

UserMatcher::matchUser()   D

Complexity

Conditions 9
Paths 15

Size

Total Lines 29
Code Lines 17

Duplication

Lines 24
Ratio 82.76 %

Importance

Changes 0
Metric Value
dl 24
loc 29
c 0
b 0
f 0
rs 4.909
cc 9
eloc 17
nc 15
nop 1
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
17
    protected $allowedConditions = array(
18
        self::MATCH_AND, self::MATCH_OR,
19
        self::MATCH_USER_ID, self::MATCH_USER_LOGIN, self::MATCH_USER_EMAIL,
20
        // aliases
21
        'id'
22
    );
23
    protected $returns = 'User';
24
25
    /**
26
     * @param array $conditions key: condition, value: int / string / int[] / string[]
27
     * @return UserCollection
28
     */
29
    public function match(array $conditions)
30
    {
31
        return $this->matchUser($conditions);
32
    }
33
34
    /**
35
     * @param array $conditions key: condition, value: int / string / int[] / string[]
36
     * @return UserCollection
37
     */
38
    public function matchUser(array $conditions)
39
    {
40
        $this->validateConditions($conditions);
41
42 View Code Duplication
        foreach ($conditions as $key => $values) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
44
            if (!is_array($values)) {
45
                $values = array($values);
46
            }
47
48
            switch ($key) {
49
                case 'id':
50
                case self::MATCH_USER_ID:
51
                   return new UserCollection($this->findUsersById($values));
52
53
                case self::MATCH_USER_LOGIN:
54
                    return new UserCollection($this->findUsersByLogin($values));
55
56
                case self::MATCH_USER_EMAIL:
57
                    return new UserCollection($this->findUsersByEmail($values));
58
59
                case self::MATCH_AND:
60
                    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 60 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...\UserMatcher::matchUser of type Kaliop\eZMigrationBundle...ion\UserCollection|null.
Loading history...
61
62
                case self::MATCH_OR:
63
                    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 63 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...\UserMatcher::matchUser of type Kaliop\eZMigrationBundle...ion\UserCollection|null.
Loading history...
64
            }
65
        }
66
    }
67
68
    /**
69
     * NB: bad luck if user login contains an email or otherwise @ character
70
     * @param string $key
71
     * @return array
72
     */
73
    protected function getConditionsFromKey($key)
74
    {
75
        if (is_int($key) || ctype_digit($key)) {
76
            return array(self::MATCH_USER_ID => $key);
77
        }
78
        if (strpos($key, '@') !== false) {
79
            return array(self::MATCH_USER_EMAIL => $key);
80
        }
81
        return array(self::MATCH_USER_LOGIN => $key);
82
    }
83
84
    /**
85
     * @param int[] $userIds
86
     * @return User[]
87
     */
88
    protected function findUsersById(array $userIds)
89
    {
90
        $users = [];
91
92
        foreach ($userIds as $userId) {
93
            // return unique contents
94
            $user = $this->repository->getUserService()->loadUser($userId);
95
96
            $users[$user->id] = $user;
97
        }
98
99
        return $users;
100
    }
101
102
    /**
103
     * @param string[] $logins
104
     * @return User[]
105
     */
106
    protected function findUsersByLogin(array $logins)
107
    {
108
        $users = [];
109
110
        foreach ($logins as $login) {
111
            // return unique contents
112
            $user = $this->repository->getUserService()->loadUserByLogin($login);
113
114
            $users[$user->id] = $user;
115
        }
116
117
        return $users;
118
    }
119
120
    /**
121
     * @param string[] $emails
122
     * @return User[]
123
     *
124
     * @todo check if this fails when user is not found
125
     */
126
    protected function findUsersByEmail(array $emails)
127
    {
128
        $users = [];
129
130
        foreach ($emails as $email) {
131
            // return unique contents
132
            $matches = $this->repository->getUserService()->loadUsersByEmail($email);
133
134
            foreach ($matches as $user) {
135
                $users[$user->id] = $user;
136
            }
137
        }
138
139
        return $users;
140
    }
141
}
142