Completed
Push — master ( 216072...caf6f7 )
by Team eFabrica
02:21
created

FilePathToKeyTokenModifier   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A modifyAll() 0 12 2
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TokenModifier;
4
5
use Efabrica\TranslationsAutomatization\Tokenizer\TokenCollection;
6
7
class FilePathToKeyTokenModifier implements TokenModifierInterface
8
{
9
    private $basePath;
10
11
    private $excludeDirectoryNames;
12
13 6
    public function __construct(string $basePath, array $excludeDirectoryNames = [])
14
    {
15 6
        $this->basePath = rtrim($basePath, '/');
16 6
        $this->excludeDirectoryNames = array_map('strtolower', $excludeDirectoryNames);
17 6
    }
18
19 6
    public function modifyAll(TokenCollection $tokenCollection): TokenCollection
20
    {
21 6
        $pathParts = explode('/', str_replace($this->basePath . '/', '', $tokenCollection->getFilePath()));
22 6
        array_pop($pathParts);
23 6
        foreach ($tokenCollection->getTokens() as $token) {
24
            $newKeyParts = array_filter(array_map('strtolower', $pathParts), function ($pathPart) {
25 6
                return !in_array($pathPart, $this->excludeDirectoryNames);
26 6
            });
27 6
            $newKeyParts[] = $token->getTranslationKey();
28 6
            $token->changeTranslationKey(implode('.', $newKeyParts));
29
        }
30 6
        return $tokenCollection;
31
    }
32
}
33