Code Duplication    Length = 108-110 lines in 3 locations

Core/Matcher/ContentTypeGroupMatcher.php 1 location

@@ 9-116 (lines=108) @@
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,
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
        }
60
    }
61
62
    protected function getConditionsFromKey($key)
63
    {
64
        if (is_int($key) || ctype_digit($key)) {
65
            return array(self::MATCH_CONTENTTYPEGROUP_ID => $key);
66
        }
67
        return array(self::MATCH_CONTENTTYPEGROUP_IDENTIFIER => $key);
68
    }
69
70
    /**
71
     * @param int[] $contentTypeGroupIds
72
     * @return ContentTypeGroup[]
73
     */
74
    protected function findContentTypeGroupsById(array $contentTypeGroupIds)
75
    {
76
        $contentTypeGroups = [];
77
78
        foreach ($contentTypeGroupIds as $contentTypeGroupId) {
79
            // return unique contents
80
            $contentTypeGroup = $this->repository->getContentTypeService()->loadContentTypeGroup($contentTypeGroupId);
81
            $contentTypeGroups[$contentTypeGroup->id] = $contentTypeGroup;
82
        }
83
84
        return $contentTypeGroups;
85
    }
86
87
    /**
88
     * @param string[] $contentTypeGroupIdentifiers
89
     * @return ContentTypeGroup[]
90
     */
91
    protected function findContentTypeGroupsByIdentifier(array $contentTypeGroupIdentifiers)
92
    {
93
        $contentTypeGroups = [];
94
95
        foreach ($contentTypeGroupIdentifiers as $contentTypeGroupIdentifier) {
96
            // return unique contents
97
            $contentTypeGroup = $this->repository->getContentTypeService()->loadContentTypeGroupByIdentifier($contentTypeGroupIdentifier);
98
            $contentTypeGroups[$contentTypeGroup->id] = $contentTypeGroup;
99
        }
100
101
        return $contentTypeGroups;
102
    }
103
104
    /**
105
     * @return ContentTypeGroup[]
106
     */
107
    protected function findAllContentTypeGroups()
108
    {
109
        $contentTypeGroups = [];
110
        foreach ($this->repository->getContentTypeService()->loadContentTypeGroups() as $contentTypeGroup) {
111
            $contentTypeGroups[$contentTypeGroup->id] = $contentTypeGroup;
112
        }
113
114
        return $contentTypeGroups;
115
    }
116
}
117

Core/Matcher/RoleMatcher.php 1 location

@@ 9-118 (lines=110) @@
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,
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
        }
60
    }
61
62
    protected function getConditionsFromKey($key)
63
    {
64
        if (is_int($key) || ctype_digit($key)) {
65
            return array(self::MATCH_ROLE_ID => $key);
66
        }
67
        return array(self::MATCH_ROLE_IDENTIFIER => $key);
68
    }
69
70
    /**
71
     * @param int[] $roleIds
72
     * @return Role[]
73
     */
74
    protected function findRolesById(array $roleIds)
75
    {
76
        $roles = [];
77
78
        foreach ($roleIds as $roleId) {
79
            // return unique contents
80
            $role = $this->repository->getRoleService()->loadRole($roleId);
81
            $roles[$role->id] = $role;
82
        }
83
84
        return $roles;
85
    }
86
87
    /**
88
     * @param string[] $roleIdentifiers
89
     * @return Role[]
90
     */
91
    protected function findRolesByIdentifier(array $roleIdentifiers)
92
    {
93
        $roles = [];
94
95
        foreach ($roleIdentifiers as $roleIdentifier) {
96
            // return unique contents
97
            $role = $this->repository->getRoleService()->loadRoleByIdentifier($roleIdentifier);
98
            $roles[$role->id] = $role;
99
        }
100
101
        return $roles;
102
    }
103
104
    /**
105
     * @return Role[]
106
     */
107
    protected function findAllRoles()
108
    {
109
        $roles = [];
110
111
        foreach ($this->repository->getRoleService()->loadRoles() as $role) {
112
            // return unique contents
113
            $roles[$role->id] = $role;
114
        }
115
116
        return $roles;
117
    }
118
}
119

Core/Matcher/SectionMatcher.php 1 location

@@ 9-118 (lines=110) @@
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,
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
        }
60
    }
61
62
    protected function getConditionsFromKey($key)
63
    {
64
        if (is_int($key) || ctype_digit($key)) {
65
            return array(self::MATCH_SECTION_ID => $key);
66
        }
67
        return array(self::MATCH_SECTION_IDENTIFIER => $key);
68
    }
69
70
    /**
71
     * @param int[] $sectionIds
72
     * @return Section[]
73
     */
74
    protected function findSectionsById(array $sectionIds)
75
    {
76
        $sections = [];
77
78
        foreach ($sectionIds as $sectionId) {
79
            // return unique contents
80
            $section = $this->repository->getSectionService()->loadSection($sectionId);
81
            $sections[$section->id] = $section;
82
        }
83
84
        return $sections;
85
    }
86
87
    /**
88
     * @param string[] $sectionIdentifiers
89
     * @return Section[]
90
     */
91
    protected function findSectionsByIdentifier(array $sectionIdentifiers)
92
    {
93
        $sections = [];
94
95
        foreach ($sectionIdentifiers as $sectionIdentifier) {
96
            // return unique contents
97
            $section = $this->repository->getSectionService()->loadSectionByIdentifier($sectionIdentifier);
98
            $sections[$section->id] = $section;
99
        }
100
101
        return $sections;
102
    }
103
104
    /**
105
     * @return Section[]
106
     */
107
    protected function findAllSections()
108
    {
109
        $sections = [];
110
111
        foreach ($this->repository->getSectionService()->loadSections() as $section) {
112
            // return unique contents
113
            $sections[$section->id] = $section;
114
        }
115
116
        return $sections;
117
    }
118
}
119