Completed
Push — master ( 5163a0...712d21 )
by Valentyn
06:59
created

TranslatableTrait::addTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Translation;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
/**
8
 * Trait TranslatableTrait
9
 * @package App\Translation
10
 * @property ArrayCollection $translations
11
 */
12
trait TranslatableTrait
13
{
14
    private $isTranslationsMappedByLocale = false;
15
16 4
    public function updateTranslations(array $translations, callable $add, callable $update = null)
17
    {
18 4
        foreach ($translations as $translation) {
19 4
            $locale = is_object($translation) ? $translation->getLocale() : $translation['locale'];
20 4
            if (null === $oldTranslation = $this->getTranslation($locale, false)) {
21 3
                $add($translation);
22 3
                continue;
23
            }
24
25 1
            if ($update === null) {
26
                // This will called only when there's update action and $update function not defined
27
                // But you still can keep it as null if you creating entity
28
                throw new \InvalidArgumentException('Unexpected behavior: founded old translation but $update function not defined');
29
            }
30
31 1
            $update($translation, $oldTranslation);
32
        }
33 4
    }
34
35 4
    public function addTranslation(EntityTranslationInterface $translation): self
36
    {
37 4
        $this->translations->set($translation->getLocale(), $translation);
38
39 4
        return $this;
40
    }
41
42
    /**
43
     * @return array
44
     */
45 10
    public function getTranslations(): array
46
    {
47 10
        if ($this->isTranslationsMappedByLocale === true) {
48 4
            return $this->translations->toArray();
49
        }
50
51 8
        $this->mapTranslationsByLocale();
52 8
        return $this->translations->toArray();
53
    }
54
55 5
    public function getTranslation(string $locale, bool $useFallbackLocale = true): ?EntityTranslationInterface
56
    {
57 5
        if ($this->isTranslationsMappedByLocale === false) {
58 5
            $this->mapTranslationsByLocale();
59
        }
60
61 5
        $translation = $this->translations->get($locale);
62
63 5
        if ($translation === null && $useFallbackLocale === true) {
64 1
            return $this->getFallbackTranslation();
65
        }
66
67 5
        return $translation;
68
    }
69
70 1
    private function getFallbackTranslation()
71
    {
72 1
        if (null === $translation = $this->translations->first()) {
73
            throw new \ErrorException(sprintf('You are trying to get translation for %s with ID %s but there\'s no translations found.', self::class, $this->id));
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
        }
75
76 1
        return $translation;
77
    }
78
79 11
    private function mapTranslationsByLocale(): void
80
    {
81 11
        $mappedTranslations = new ArrayCollection();
82
83 11
        foreach ($this->translations as $translation) {
84 9
            $mappedTranslations->set($translation->getLocale(), $translation);
85
        }
86
87 11
        $this->isTranslationsMappedByLocale = true;
88 11
        $this->translations = $mappedTranslations;
89
    }
90
}