Code Duplication    Length = 118-119 lines in 4 locations

Core/Matcher/ContentTypeGroupMatcher.php 1 location

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

Core/Matcher/RoleMatcher.php 1 location

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

Core/Matcher/SectionMatcher.php 1 location

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

Core/Matcher/LanguageMatcher.php 1 location

@@ 9-127 (lines=119) @@
6
use Kaliop\eZMigrationBundle\API\Collection\LanguageCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9
class LanguageMatcher extends RepositoryMatcher implements KeyMatcherInterface
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_LANGUAGE_ID = 'language_id';
14
    const MATCH_LANGUAGE_CODE = 'language_code';
15
16
    protected $allowedConditions = array(
17
        self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
18
        self::MATCH_LANGUAGE_ID, self::MATCH_LANGUAGE_CODE,
19
        // aliases
20
        'id', 'language'
21
    );
22
    protected $returns = 'Language';
23
24
    /**
25
     * @param array $conditions key: condition, value: int / string / int[] / string[]
26
     * @return LanguageCollection
27
     */
28
    public function match(array $conditions)
29
    {
30
        return $this->matchLanguage($conditions);
31
    }
32
33
    /**
34
     * @param array $conditions key: condition, value: int / string / int[] / string[]
35
     * @return LanguageCollection
36
     */
37
    public function matchLanguage(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_LANGUAGE_ID:
50
                    return new LanguageCollection($this->findLanguagesById($values));
51
52
                case 'langauge_code':
53
                case self::MATCH_LANGUAGE_CODE:
54
                    return new LanguageCollection($this->findLanguagesByCode($values));
55
56
                case self::MATCH_ALL:
57
                    return new LanguageCollection($this->findAllLanguages());
58
59
                case self::MATCH_AND:
60
                    return $this->matchAnd($values);
61
62
                case self::MATCH_OR:
63
                    return $this->matchOr($values);
64
65
                case self::MATCH_NOT:
66
                    return new LanguageCollection(array_diff_key($this->findAllLanguages(), $this->matchLanguage($values)->getArrayCopy()));
67
            }
68
        }
69
    }
70
71
    protected function getConditionsFromKey($key)
72
    {
73
        if (is_int($key) || ctype_digit($key)) {
74
            return array(self::MATCH_LANGUAGE_ID => $key);
75
        }
76
        return array(self::MATCH_LANGUAGE_CODE => $key);
77
    }
78
79
    /**
80
     * @param int[] $languageIds
81
     * @return Language[]
82
     */
83
    protected function findLanguagesById(array $languageIds)
84
    {
85
        $languages = [];
86
87
        foreach ($languageIds as $languageId) {
88
            // return unique contents
89
            $language = $this->repository->getContentLanguageService()->loadLanguageById($languageId);
90
            $languages[$language->id] = $language;
91
        }
92
93
        return $languages;
94
    }
95
96
    /**
97
     * @param string[] $languageIdentifiers
98
     * @return Language[]
99
     */
100
    protected function findLanguagesByCode(array $languageIdentifiers)
101
    {
102
        $languages = [];
103
104
        foreach ($languageIdentifiers as $languageIdentifier) {
105
            // return unique contents
106
            $language = $this->repository->getContentLanguageService()->loadLanguage($languageIdentifier);
107
            $languages[$language->id] = $language;
108
        }
109
110
        return $languages;
111
    }
112
113
    /**
114
     * @return Language[]
115
     */
116
    protected function findAllLanguages()
117
    {
118
        $languages = [];
119
120
        foreach ($this->repository->getLanguageService()->loadLanguages() as $language) {
121
            // return unique contents
122
            $languages[$language->id] = $language;
123
        }
124
125
        return $languages;
126
    }
127
}
128