BingTranslateTokenModifier::modifyAll()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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