Completed
Pull Request — master (#21)
by Valentyn
01:42
created

TranslatedEntityHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 20
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateTranslations() 0 17 4
1
<?php
2
3
namespace App\Translation;
4
5
class TranslatedEntityHelper
6
{
7 2
    public function updateTranslations(TranslatableInterface $entity, array $translations, callable $add, callable $update = null)
8
    {
9 2
        foreach ($translations as $translation) {
10 2
            if (null === $oldTranslation = $entity->getTranslation($translation['locale'], false)) {
11 2
                $add($translation);
12 2
                continue;
13
            }
14
15 1
            if ($update === null) {
16
                // This will called only when there's update action and $update function not defined
17
                // But you still can keep it as null if you creating entity
18
                throw new \InvalidArgumentException('Unexpected behavior: founded old translation but $update function not defined');
19
            }
20
21 1
            $update($translation, $oldTranslation);
22
        }
23
    }
24
}