Code Duplication    Length = 111-113 lines in 3 locations

Core/Matcher/ContentTypeGroupMatcher.php 1 location

@@ 9-119 (lines=111) @@
6
use Kaliop\eZMigrationBundle\API\Collection\ContentTypeGroupCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9
class ContentTypeGroupMatcher extends RepositoryMatcher implements KeyMatcherInterface
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_CONTENTTYPEGROUP_ID = 'contenttypegroup_id';
14
    const MATCH_CONTENTTYPEGROUP_IDENTIFIER = 'contenttypegroup_identifier';
15
16
    protected $allowedConditions = array(
17
        self::MATCH_ALL, self::MATCH_NOT,
18
        self::MATCH_CONTENTTYPEGROUP_ID, self::MATCH_CONTENTTYPEGROUP_IDENTIFIER,
19
        // aliases
20
        'id', 'identifier'
21
    );
22
    protected $returns = 'ContentTypeGroup';
23
24
    /**
25
     * @param array $conditions key: condition, value: int / string / int[] / string[]
26
     * @return ContentTypeGroupCollection
27
     */
28
    public function match(array $conditions)
29
    {
30
        return $this->matchContentTypeGroup($conditions);
31
    }
32
33
    /**
34
     * @param array $conditions key: condition, value: int / string / int[] / string[]
35
     * @return ContentTypeGroupCollection
36
     */
37
    public function matchContentTypeGroup(array $conditions)
38
    {
39
        $this->validateConditions($conditions);
40
41
        foreach ($conditions as $key => $values) {
42
43
            if (!is_array($values)) {
44
                $values = array($values);
45
            }
46
47
            switch ($key) {
48
                case 'id':
49
                case self::MATCH_CONTENTTYPEGROUP_ID:
50
                    return new ContentTypeGroupCollection($this->findContentTypeGroupsById($values));
51
52
                case 'identifier':
53
                case self::MATCH_CONTENTTYPEGROUP_IDENTIFIER:
54
                    return new ContentTypeGroupCollection($this->findContentTypeGroupsByIdentifier($values));
55
56
                case self::MATCH_ALL:
57
                    return new ContentTypeGroupCollection($this->findAllContentTypeGroups());
58
59
                case self::MATCH_NOT:
60
                    return new ContentTypeGroupCollection(array_diff_key($this->findAllContentTypeGroups(), $this->matchContentTypeGroup($values)->getArrayCopy()));
61
            }
62
        }
63
    }
64
65
    protected function getConditionsFromKey($key)
66
    {
67
        if (is_int($key) || ctype_digit($key)) {
68
            return array(self::MATCH_CONTENTTYPEGROUP_ID => $key);
69
        }
70
        return array(self::MATCH_CONTENTTYPEGROUP_IDENTIFIER => $key);
71
    }
72
73
    /**
74
     * @param int[] $contentTypeGroupIds
75
     * @return ContentTypeGroup[]
76
     */
77
    protected function findContentTypeGroupsById(array $contentTypeGroupIds)
78
    {
79
        $contentTypeGroups = [];
80
81
        foreach ($contentTypeGroupIds as $contentTypeGroupId) {
82
            // return unique contents
83
            $contentTypeGroup = $this->repository->getContentTypeService()->loadContentTypeGroup($contentTypeGroupId);
84
            $contentTypeGroups[$contentTypeGroup->id] = $contentTypeGroup;
85
        }
86
87
        return $contentTypeGroups;
88
    }
89
90
    /**
91
     * @param string[] $contentTypeGroupIdentifiers
92
     * @return ContentTypeGroup[]
93
     */
94
    protected function findContentTypeGroupsByIdentifier(array $contentTypeGroupIdentifiers)
95
    {
96
        $contentTypeGroups = [];
97
98
        foreach ($contentTypeGroupIdentifiers as $contentTypeGroupIdentifier) {
99
            // return unique contents
100
            $contentTypeGroup = $this->repository->getContentTypeService()->loadContentTypeGroupByIdentifier($contentTypeGroupIdentifier);
101
            $contentTypeGroups[$contentTypeGroup->id] = $contentTypeGroup;
102
        }
103
104
        return $contentTypeGroups;
105
    }
106
107
    /**
108
     * @return ContentTypeGroup[]
109
     */
110
    protected function findAllContentTypeGroups()
111
    {
112
        $contentTypeGroups = [];
113
        foreach ($this->repository->getContentTypeService()->loadContentTypeGroups() as $contentTypeGroup) {
114
            $contentTypeGroups[$contentTypeGroup->id] = $contentTypeGroup;
115
        }
116
117
        return $contentTypeGroups;
118
    }
119
}
120

Core/Matcher/RoleMatcher.php 1 location

@@ 9-121 (lines=113) @@
6
use Kaliop\eZMigrationBundle\API\Collection\RoleCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9
class RoleMatcher extends RepositoryMatcher implements KeyMatcherInterface
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_ROLE_ID = 'role_id';
14
    const MATCH_ROLE_IDENTIFIER = 'role_identifier';
15
16
    protected $allowedConditions = array(
17
        self::MATCH_ALL, self::MATCH_NOT,
18
        self::MATCH_ROLE_ID, self::MATCH_ROLE_IDENTIFIER,
19
        // aliases
20
        'id', 'identifier'
21
    );
22
    protected $returns = 'Role';
23
24
    /**
25
     * @param array $conditions key: condition, value: int / string / int[] / string[]
26
     * @return RoleCollection
27
     */
28
    public function match(array $conditions)
29
    {
30
        return $this->matchRole($conditions);
31
    }
32
33
    /**
34
     * @param array $conditions key: condition, value: int / string / int[] / string[]
35
     * @return RoleCollection
36
     */
37
    public function matchRole(array $conditions)
38
    {
39
        $this->validateConditions($conditions);
40
41
        foreach ($conditions as $key => $values) {
42
43
            if (!is_array($values)) {
44
                $values = array($values);
45
            }
46
47
            switch ($key) {
48
                case 'id':
49
                case self::MATCH_ROLE_ID:
50
                   return new RoleCollection($this->findRolesById($values));
51
52
                case 'identifier':
53
                case self::MATCH_ROLE_IDENTIFIER:
54
                    return new RoleCollection($this->findRolesByIdentifier($values));
55
56
                case self::MATCH_ALL:
57
                    return new RoleCollection($this->findAllRoles());
58
59
                case self::MATCH_NOT:
60
                    return new RoleCollection(array_diff_key($this->findAllRoles(), $this->matchRole($values)->getArrayCopy()));
61
            }
62
        }
63
    }
64
65
    protected function getConditionsFromKey($key)
66
    {
67
        if (is_int($key) || ctype_digit($key)) {
68
            return array(self::MATCH_ROLE_ID => $key);
69
        }
70
        return array(self::MATCH_ROLE_IDENTIFIER => $key);
71
    }
72
73
    /**
74
     * @param int[] $roleIds
75
     * @return Role[]
76
     */
77
    protected function findRolesById(array $roleIds)
78
    {
79
        $roles = [];
80
81
        foreach ($roleIds as $roleId) {
82
            // return unique contents
83
            $role = $this->repository->getRoleService()->loadRole($roleId);
84
            $roles[$role->id] = $role;
85
        }
86
87
        return $roles;
88
    }
89
90
    /**
91
     * @param string[] $roleIdentifiers
92
     * @return Role[]
93
     */
94
    protected function findRolesByIdentifier(array $roleIdentifiers)
95
    {
96
        $roles = [];
97
98
        foreach ($roleIdentifiers as $roleIdentifier) {
99
            // return unique contents
100
            $role = $this->repository->getRoleService()->loadRoleByIdentifier($roleIdentifier);
101
            $roles[$role->id] = $role;
102
        }
103
104
        return $roles;
105
    }
106
107
    /**
108
     * @return Role[]
109
     */
110
    protected function findAllRoles()
111
    {
112
        $roles = [];
113
114
        foreach ($this->repository->getRoleService()->loadRoles() as $role) {
115
            // return unique contents
116
            $roles[$role->id] = $role;
117
        }
118
119
        return $roles;
120
    }
121
}
122

Core/Matcher/SectionMatcher.php 1 location

@@ 9-121 (lines=113) @@
6
use Kaliop\eZMigrationBundle\API\Collection\SectionCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9
class SectionMatcher extends RepositoryMatcher implements KeyMatcherInterface
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_SECTION_ID = 'section_id';
14
    const MATCH_SECTION_IDENTIFIER = 'section_identifier';
15
16
    protected $allowedConditions = array(
17
        self::MATCH_ALL, self::MATCH_NOT,
18
        self::MATCH_SECTION_ID, self::MATCH_SECTION_IDENTIFIER,
19
        // aliases
20
        'id', 'identifier'
21
    );
22
    protected $returns = 'Role';
23
24
    /**
25
     * @param array $conditions key: condition, value: int / string / int[] / string[]
26
     * @return SectionCollection
27
     */
28
    public function match(array $conditions)
29
    {
30
        return $this->matchSection($conditions);
31
    }
32
33
    /**
34
     * @param array $conditions key: condition, value: int / string / int[] / string[]
35
     * @return SectionCollection
36
     */
37
    public function matchSection(array $conditions)
38
    {
39
        $this->validateConditions($conditions);
40
41
        foreach ($conditions as $key => $values) {
42
43
            if (!is_array($values)) {
44
                $values = array($values);
45
            }
46
47
            switch ($key) {
48
                case 'id':
49
                case self::MATCH_SECTION_ID:
50
                   return new SectionCollection($this->findSectionsById($values));
51
52
                case 'identifier':
53
                case self::MATCH_SECTION_IDENTIFIER:
54
                    return new SectionCollection($this->findSectionsByIdentifier($values));
55
56
                case self::MATCH_ALL:
57
                    return new SectionCollection($this->findAllSections());
58
59
                case self::MATCH_NOT:
60
                    return new SectionCollection(array_diff_key($this->findAllSections(), $this->matchSection($values)->getArrayCopy()));
61
            }
62
        }
63
    }
64
65
    protected function getConditionsFromKey($key)
66
    {
67
        if (is_int($key) || ctype_digit($key)) {
68
            return array(self::MATCH_SECTION_ID => $key);
69
        }
70
        return array(self::MATCH_SECTION_IDENTIFIER => $key);
71
    }
72
73
    /**
74
     * @param int[] $sectionIds
75
     * @return Section[]
76
     */
77
    protected function findSectionsById(array $sectionIds)
78
    {
79
        $sections = [];
80
81
        foreach ($sectionIds as $sectionId) {
82
            // return unique contents
83
            $section = $this->repository->getSectionService()->loadSection($sectionId);
84
            $sections[$section->id] = $section;
85
        }
86
87
        return $sections;
88
    }
89
90
    /**
91
     * @param string[] $sectionIdentifiers
92
     * @return Section[]
93
     */
94
    protected function findSectionsByIdentifier(array $sectionIdentifiers)
95
    {
96
        $sections = [];
97
98
        foreach ($sectionIdentifiers as $sectionIdentifier) {
99
            // return unique contents
100
            $section = $this->repository->getSectionService()->loadSectionByIdentifier($sectionIdentifier);
101
            $sections[$section->id] = $section;
102
        }
103
104
        return $sections;
105
    }
106
107
    /**
108
     * @return Section[]
109
     */
110
    protected function findAllSections()
111
    {
112
        $sections = [];
113
114
        foreach ($this->repository->getSectionService()->loadSections() as $section) {
115
            // return unique contents
116
            $sections[$section->id] = $section;
117
        }
118
119
        return $sections;
120
    }
121
}
122