Completed
Push — master ( e26eba...178ca9 )
by Eric
37:26 queued 30:42
created

TranslatableTrait::setTranslationFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Translation\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Lug\Component\Resource\Factory\FactoryInterface;
17
use Lug\Component\Translation\Exception\LocaleNotFoundException;
18
use Lug\Component\Translation\Exception\TranslationNotFoundException;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
trait TranslatableTrait
24
{
25
    /**
26
     * @var string[]
27
     */
28
    private $locales;
29
30
    /**
31
     * @var string|null
32
     */
33
    private $fallbackLocale;
34
35
    /**
36
     * @var FactoryInterface
37
     */
38
    private $translationFactory;
39
40
    /**
41
     * @var TranslationInterface[]|Collection
42
     */
43
    protected $translations;
44
45
    /**
46
     * @return string|null
47
     */
48 3
    public function getLocale()
49
    {
50
        try {
51 3
            return $this->getTranslation()->getLocale();
52 2
        } catch (TranslationNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
53
        }
54 2
    }
55
56
    /**
57
     * @return string[]
58
     */
59 13
    public function getLocales()
60
    {
61 13
        return $this->locales;
62
    }
63
64
    /**
65
     * @param string[] $locales
66
     */
67 11
    public function setLocales($locales)
68
    {
69 11
        $this->locales = $locales;
70 11
    }
71
72
    /**
73
     * @return bool
74
     */
75 7
    public function hasFallbackLocale()
76
    {
77 7
        return $this->fallbackLocale !== null;
78
    }
79
80
    /**
81
     * @return string|null
82
     */
83 7
    public function getFallbackLocale()
84
    {
85 7
        return $this->fallbackLocale;
86
    }
87
88
    /**
89
     * @param string|null $fallbackLocale
90
     */
91 8
    public function setFallbackLocale($fallbackLocale)
92
    {
93 8
        $this->fallbackLocale = $fallbackLocale;
94 8
    }
95
96
    /**
97
     * @return FactoryInterface
98
     */
99 6
    public function getTranslationFactory()
100
    {
101 6
        return $this->translationFactory;
102
    }
103
104
    /**
105
     * @param FactoryInterface $translationFactory
106
     */
107 5
    public function setTranslationFactory(FactoryInterface $translationFactory)
108
    {
109 5
        $this->translationFactory = $translationFactory;
110 5
    }
111
112
    /**
113
     * @return TranslationInterface[]|Collection
114
     */
115 5
    public function getTranslations()
116
    {
117 5
        return $this->translations;
118
    }
119
120
    /**
121
     * @param bool $allowCreate
122
     *
123
     * @return TranslationInterface
124
     */
125 10
    public function getTranslation($allowCreate = false)
126
    {
127 10
        $locales = $this->getLocales();
128
129 10
        if (empty($locales)) {
130 1
            throw new LocaleNotFoundException();
131
        }
132
133 9
        $translation = null;
134
135 9
        foreach ($locales as $locale) {
136 9
            if (($translation = $this->translations->get($locale)) !== null) {
137 3
                break;
138
            }
139 9
        }
140
141 9
        if ($translation === null && $allowCreate) {
142 2
            $translation = $this->getTranslationFactory()->create(['locale' => reset($locales)]);
143 2
            $this->addTranslation($translation);
144 2
        }
145
146 9
        if ($translation === null && $this->hasFallbackLocale()) {
147 3
            $translation = $this->translations->get($this->getFallbackLocale());
148 3
        }
149
150 9
        if ($translation === null) {
151 4
            if ($this->hasFallbackLocale()) {
152 2
                $locales[] = $this->getFallbackLocale();
153 2
            }
154
155 4
            throw new TranslationNotFoundException();
156
        }
157
158 5
        return $translation;
159
    }
160
161
    /**
162
     * @param TranslationInterface[]|Collection $translations
163
     */
164
    public function setTranslations($translations)
165
    {
166
        $this->translations->clear();
167
168
        foreach ($translations as $translation) {
169
            $this->addTranslation($translation);
170
        }
171
    }
172
173
    /**
174
     * @param TranslationInterface $translation
175
     */
176 9
    public function addTranslation(TranslationInterface $translation)
177
    {
178 9
        if (!$this->translations->containsKey($translation->getLocale())) {
179 9
            $this->translations->set($translation->getLocale(), $translation);
180 9
            $translation->setTranslatable($this);
181 9
        }
182 9
    }
183
184
    /**
185
     * @param TranslationInterface $translation
186
     */
187 2
    public function removeTranslation(TranslationInterface $translation)
188
    {
189 2
        if ($this->translations->removeElement($translation)) {
190 1
            $translation->setTranslatable(null);
191 1
        }
192 2
    }
193
194 19
    private function initTranslatable()
195
    {
196 19
        $this->translations = new ArrayCollection();
197 19
    }
198
}
199