Passed
Pull Request — master (#13)
by Michal
01:38
created

BingTranslateTokenModifier::modifyAll()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TokenModifier;
4
5
use Efabrica\TranslationsAutomatization\Tokenizer\TokenCollection;
6
use Efabrica\TranslationsAutomatization\Translator\BingTranslator;
7
8
class BingTranslateTokenModifier implements TokenModifierInterface
9
{
10
    private $translator;
11
12 8
    public function __construct(string $from, string $to)
13
    {
14 8
        $this->translator = new BingTranslator($from, $to);
15 8
    }
16
17 6
    public function modifyAll(TokenCollection $tokenCollection): TokenCollection
18
    {
19 6
        $oldKeys = [];
20 6
        foreach ($tokenCollection->getTokens() as $token) {
21 4
            $oldKeys[] = $token->getTranslationKey();
22
        }
23
24 6
        if (empty($oldKeys)) {
25 2
            return $tokenCollection;
26
        }
27
28 4
        $oldToNewKeys = $this->translator->translate($oldKeys);
29 4
        foreach ($tokenCollection->getTokens() as $token) {
30 4
            $token->changeTranslationKey($oldToNewKeys[$token->getTranslationKey()] ?? $token->getTranslationKey());
31
        }
32 4
        return $tokenCollection;
33
    }
34
}
35