Completed
Push — master ( 397eee...f113dc )
by Gaetano
22:13 queued 10:37
created

LanguageMatcher::matchLanguage()   B

Complexity

Conditions 11
Paths 19

Size

Total Lines 33

Duplication

Lines 33
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 33
loc 33
ccs 0
cts 25
cp 0
rs 7.3166
c 0
b 0
f 0
cc 11
nc 19
nop 1
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\Content\Language;
6
use Kaliop\eZMigrationBundle\API\Collection\LanguageCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
9 View Code Duplication
class LanguageMatcher extends RepositoryMatcher implements KeyMatcherInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
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...eMatcher::matchLanguage of type Kaliop\eZMigrationBundle...LanguageCollection|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...eMatcher::matchLanguage of type Kaliop\eZMigrationBundle...LanguageCollection|null.
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The method getLanguageService() does not seem to exist on object<eZ\Publish\API\Repository\Repository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
            // return unique contents
122
            $languages[$language->id] = $language;
123
        }
124
125
        return $languages;
126
    }
127
}
128