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

TranslatedEntityHelper::updateTranslations()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 4
crap 4.0218
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
}