Completed
Pull Request — master (#90)
by Andreas
10:43
created

findUserGroupsByContentRemoteIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
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_USERGROUP_ID,
21
        self::MATCH_CONTENT_REMOTE_ID,
22
        // aliases
23
        'id'
24
    );
25
    protected $returns = 'UserGroup';
26
27
    /**
28
     * @param array $conditions key: condition, value: int / string / int[] / string[]
29
     * @return UserGroupCollection
30
     */
31
    public function match(array $conditions)
32
    {
33
        return $this->matchUserGroup($conditions);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchUserGroup($conditions); of type Kaliop\eZMigrationBundle...serGroupCollection|null adds the type Kaliop\eZMigrationBundle...ion\UserGroupCollection to the return on line 33 which is incompatible with the return type declared by the interface Kaliop\eZMigrationBundle...MatcherInterface::match of type array|Kaliop\eZMigrationBundle\API\ArrayObject.
Loading history...
34
    }
35
36
    /**
37
     * @param array $conditions key: condition, value: int / string / int[] / string[]
38
     * @return UserGroupCollection
39
     */
40
    public function matchUserGroup(array $conditions)
41
    {
42
        $this->validateConditions($conditions);
43
44
        foreach ($conditions as $key => $values) {
45
46
            if (!is_array($values)) {
47
                $values = array($values);
48
            }
49
50
            switch ($key) {
51
                case 'id':
52
                case self::MATCH_USERGROUP_ID:
53
                    return new UserGroupCollection($this->findUserGroupsById($values));
54
                case self::MATCH_CONTENT_REMOTE_ID:
55
                    return new UserGroupCollection($this->findUserGroupsByContentRemoteIds($values));
56
57
            }
58
        }
59
    }
60
61
    /**
62
     * When matching by key, we accept user group Id and it's remote Id only
63
     * @param int|string $key
64
     * @return array
65
     */
66
    protected function getConditionsFromKey($key)
67
    {
68
        if (is_int($key) || ctype_digit($key)) {
69
            return array(self::MATCH_USERGROUP_ID => $key);
70
        }
71
        return array(self::MATCH_CONTENT_REMOTE_ID => $key);
72
    }
73
74
    /**
75
     * @param int[] $userGroupIds
76
     * @return UserGroup[]
77
     */
78
    protected function findUserGroupsById(array $userGroupIds)
79
    {
80
        $userGroups = [];
81
82
        foreach ($userGroupIds as $userGroupId) {
83
            // return unique contents
84
            $userGroup = $this->repository->getUserService()->loadUserGroup($userGroupId);
85
86
            $userGroups[$userGroup->id] = $userGroup;
87
        }
88
89
        return $userGroups;
90
    }
91
92
    /**
93
     * @param string[] $remoteContentIds
94
     * @return UserGroup[]
95
     */
96
    protected function findUserGroupsByContentRemoteIds(array $remoteContentIds)
97
    {
98
        $userGroups = [];
99
100
        foreach ($remoteContentIds as $remoteContentId) {
101
            // return unique contents
102
103
            // user service does not provide a method to load user groups via remote_id, but as user groups are content...
104
            $content = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId);
105
            $userGroup = $this->repository->getUserService()->loadUserGroup($content->id);
106
107
            $userGroups[$userGroup->id] = $userGroup;
108
        }
109
110
        return $userGroups;
111
    }
112
}
113