Completed
Push — master ( 5a501b...3982a6 )
by Gaetano
08:04
created

UserGroupMatcher::matchUserGroup()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 20
cp 0
rs 8.4444
c 0
b 0
f 0
cc 8
nc 13
nop 1
crap 72
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\User\UserGroup;
6
use Kaliop\eZMigrationBundle\API\Collection\UserGroupCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9
/**
10
 * @todo add matching all groups of a user, all child groups of a group
11
 */
12
class UserGroupMatcher extends RepositoryMatcher implements KeyMatcherInterface
13
{
14
    use FlexibleKeyMatcherTrait;
15
16
    const MATCH_USERGROUP_ID = 'usergroup_id';
17
    const MATCH_CONTENT_REMOTE_ID = 'content_remote_id';
18
19
    protected $allowedConditions = array(
20
        self::MATCH_AND, self::MATCH_OR,
21
        self::MATCH_USERGROUP_ID,
22
        self::MATCH_CONTENT_REMOTE_ID,
23
        // aliases
24
        'id'
25
    );
26
    protected $returns = 'UserGroup';
27
28
    /**
29
     * @param array $conditions key: condition, value: int / string / int[] / string[]
30
     * @return UserGroupCollection
31
     */
32
    public function match(array $conditions)
33
    {
34
        return $this->matchUserGroup($conditions);
35
    }
36
37
    /**
38
     * @param array $conditions key: condition, value: int / string / int[] / string[]
39
     * @return UserGroupCollection
40
     */
41
    public function matchUserGroup(array $conditions)
42
    {
43
        $this->validateConditions($conditions);
44
45
        foreach ($conditions as $key => $values) {
46
47
            if (!is_array($values)) {
48
                $values = array($values);
49
            }
50
51
            switch ($key) {
52
                case 'id':
53
                case self::MATCH_USERGROUP_ID:
54
                    return new UserGroupCollection($this->findUserGroupsById($values));
55
56
                case self::MATCH_CONTENT_REMOTE_ID:
57
                    return new UserGroupCollection($this->findUserGroupsByContentRemoteIds($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...Matcher::matchUserGroup of type Kaliop\eZMigrationBundle...serGroupCollection|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...Matcher::matchUserGroup of type Kaliop\eZMigrationBundle...serGroupCollection|null.
Loading history...
64
            }
65
        }
66
    }
67
68
    /**
69
     * When matching by key, we accept user group Id and it's remote Id only
70
     * @param int|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_USERGROUP_ID => $key);
77
        }
78
        return array(self::MATCH_CONTENT_REMOTE_ID => $key);
79
    }
80
81
    /**
82
     * @param int[] $userGroupIds
83
     * @return UserGroup[]
84
     */
85
    protected function findUserGroupsById(array $userGroupIds)
86
    {
87
        $userGroups = [];
88
89
        foreach ($userGroupIds as $userGroupId) {
90
            // return unique contents
91
            $userGroup = $this->repository->getUserService()->loadUserGroup($userGroupId);
92
93
            $userGroups[$userGroup->id] = $userGroup;
94
        }
95
96
        return $userGroups;
97
    }
98
99
    /**
100
     * @param string[] $remoteContentIds
101
     * @return UserGroup[]
102
     */
103
    protected function findUserGroupsByContentRemoteIds(array $remoteContentIds)
104
    {
105
        $userGroups = [];
106
107
        foreach ($remoteContentIds as $remoteContentId) {
108
            // return unique contents
109
110
            // user service does not provide a method to load user groups via remote_id, but as user groups are content...
111
            $content = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId);
112
            $userGroup = $this->repository->getUserService()->loadUserGroup($content->id);
113
114
            $userGroups[$userGroup->id] = $userGroup;
115
        }
116
117
        return $userGroups;
118
    }
119
}
120