Completed
Push — master ( 97fb46...770b11 )
by Gaetano
15:34 queued 07:06
created

LanguageMatcher::getConditionsFromKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 12
rs 10
c 0
b 0
f 0
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
use Kaliop\eZMigrationBundle\API\Exception\InvalidMatchConditionsException;
9
10
class LanguageMatcher extends RepositoryMatcher implements KeyMatcherInterface
11
{
12
    use FlexibleKeyMatcherTrait;
13
14
    const MATCH_LANGUAGE_ID = 'language_id';
15
    const MATCH_LANGUAGE_CODE = 'language_code';
16
17
    protected $allowedConditions = array(
18
        self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
19
        self::MATCH_LANGUAGE_ID, self::MATCH_LANGUAGE_CODE,
20
        // aliases
21
        'id', 'language'
22
    );
23
    protected $returns = 'Language';
24
25
    /**
26
     * @param array $conditions key: condition, value: int / string / int[] / string[]
27
     * @return LanguageCollection
28
     * @throws InvalidMatchConditionsException
29
     */
30 4
    public function match(array $conditions)
31
    {
32 4
        return $this->matchLanguage($conditions);
33
    }
34
35
    /**
36
     * @param array $conditions key: condition, value: int / string / int[] / string[]
37
     * @return LanguageCollection
38
     * @throws InvalidMatchConditionsException
39
     */
40 4
    public function matchLanguage(array $conditions)
41
    {
42 4
        $this->validateConditions($conditions);
43
44 4
        foreach ($conditions as $key => $values) {
45
46 4
            if (!is_array($values)) {
47 4
                $values = array($values);
48
            }
49
50
            switch ($key) {
51 4
                case 'id':
52 4
                case self::MATCH_LANGUAGE_ID:
53 1
                    return new LanguageCollection($this->findLanguagesById($values));
54
55 4
                case 'langauge_code':
56 4
                case self::MATCH_LANGUAGE_CODE:
57 1
                    return new LanguageCollection($this->findLanguagesByCode($values));
58
59 3
                case self::MATCH_ALL:
60 3
                    return new LanguageCollection($this->findAllLanguages());
61
62
                case self::MATCH_AND:
63
                    return $this->matchAnd($values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->matchAnd($values) returns the type array which is incompatible with the documented return type Kaliop\eZMigrationBundle...tion\LanguageCollection.
Loading history...
64
65
                case self::MATCH_OR:
66
                    return $this->matchOr($values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->matchOr($values) returns the type array which is incompatible with the documented return type Kaliop\eZMigrationBundle...tion\LanguageCollection.
Loading history...
67
68
                case self::MATCH_NOT:
69
                    return new LanguageCollection(array_diff_key($this->findAllLanguages(), $this->matchLanguage($values)->getArrayCopy()));
70
            }
71
        }
72
    }
73
74
    protected function getConditionsFromKey($key)
75
    {
76
        if (is_int($key) || ctype_digit($key)) {
77
            return array(self::MATCH_LANGUAGE_ID => $key);
78
        }
79
        return array(self::MATCH_LANGUAGE_CODE => $key);
80
    }
81
82
    /**
83
     * @param int[] $languageIds
84
     * @return Language[]
85
     */
86 1
    protected function findLanguagesById(array $languageIds)
87
    {
88 1
        $languages = [];
89
90 1
        foreach ($languageIds as $languageId) {
91
            // return unique contents
92 1
            $language = $this->repository->getContentLanguageService()->loadLanguageById($languageId);
93 1
            $languages[$language->id] = $language;
94
        }
95
96 1
        return $languages;
97
    }
98
99
    /**
100
     * @param string[] $languageIdentifiers
101
     * @return Language[]
102
     */
103 1
    protected function findLanguagesByCode(array $languageIdentifiers)
104
    {
105 1
        $languages = [];
106
107 1
        foreach ($languageIdentifiers as $languageIdentifier) {
108
            // return unique contents
109 1
            $language = $this->repository->getContentLanguageService()->loadLanguage($languageIdentifier);
110 1
            $languages[$language->id] = $language;
111
        }
112
113 1
        return $languages;
114
    }
115
116
    /**
117
     * @return Language[]
118
     */
119 3
    protected function findAllLanguages()
120
    {
121 3
        $languages = [];
122
123 3
        foreach ($this->repository->getContentLanguageService()->loadLanguages() as $language) {
124
            // return unique contents
125 3
            $languages[$language->id] = $language;
126
        }
127
128 3
        return $languages;
129
    }
130
}
131