Completed
Push — master ( d15cf4...5163a0 )
by Valentyn
06:05
created

TranslatableTrait::getTranslation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 2
crap 4
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 3
    public function updateTranslations(array $translations, callable $add, callable $update = null)
17
    {
18 3
        foreach ($translations as $translation) {
19 3
            if (null === $oldTranslation = $this->getTranslation($translation['locale'], false)) {
20 2
                $add($translation);
21 2
                continue;
22
            }
23
24 1
            if ($update === null) {
25
                // This will called only when there's update action and $update function not defined
26
                // But you still can keep it as null if you creating entity
27
                throw new \InvalidArgumentException('Unexpected behavior: founded old translation but $update function not defined');
28
            }
29
30 1
            $update($translation, $oldTranslation);
31
        }
32 3
    }
33
34 3
    public function addTranslation(EntityTranslationInterface $translation): self
35
    {
36 3
        $this->translations->set($translation->getLocale(), $translation);
37
38 3
        return $this;
39
    }
40
41
    /**
42
     * @return array
43
     */
44 8
    public function getTranslations(): array
45
    {
46 8
        if ($this->isTranslationsMappedByLocale === true) {
47 3
            return $this->translations->toArray();
48
        }
49
50 7
        $this->mapTranslationsByLocale();
51 7
        return $this->translations->toArray();
52
    }
53
54 4
    public function getTranslation(string $locale, bool $useFallbackLocale = true): ?EntityTranslationInterface
55
    {
56 4
        if ($this->isTranslationsMappedByLocale === false) {
57 4
            $this->mapTranslationsByLocale();
58
        }
59
60 4
        $translation = $this->translations->get($locale);
61
62 4
        if ($translation === null && $useFallbackLocale === true) {
63 1
            return $this->getFallbackTranslation();
64
        }
65
66 4
        return $translation;
67
    }
68
69 1
    private function getFallbackTranslation()
70
    {
71 1
        if (null === $translation = $this->translations->first()) {
72
            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...
73
        }
74
75 1
        return $translation;
76
    }
77
78 9
    private function mapTranslationsByLocale(): void
79
    {
80 9
        $mappedTranslations = new ArrayCollection();
81
82 9
        foreach ($this->translations as $translation) {
83 8
            $mappedTranslations->set($translation->getLocale(), $translation);
84
        }
85
86 9
        $this->isTranslationsMappedByLocale = true;
87 9
        $this->translations = $mappedTranslations;
88
    }
89
}