Completed
Push — travis-trusty ( 9f6933...d115bd )
by Kamil
21:24
created

TranslatableTrait::removeTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Component\Resource\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\ORM\PersistentCollection;
18
19
/**
20
 * @see TranslatableInterface
21
 *
22
 * @author Gonzalo Vilaseca <[email protected]>
23
 */
24
trait TranslatableTrait
25
{
26
    /**
27
     * @var ArrayCollection|PersistentCollection|TranslationInterface[]
28
     */
29
    protected $translations;
30
31
    /**
32
     * @var array|TranslationInterface[]
33
     */
34
    protected $translationsCache = [];
35
36
    /**
37
     * @var string
38
     */
39
    protected $currentLocale;
40
41
    /**
42
     * Cache current translation. Useful in Doctrine 2.4+
43
     *
44
     * @var TranslationInterface
45
     */
46
    protected $currentTranslation;
47
48
    /**
49
     * @var string
50
     */
51
    protected $fallbackLocale;
52
53
    public function __construct()
54
    {
55
        $this->translations = new ArrayCollection();
56
    }
57
58
    /**
59
     * @param string $locale
0 ignored issues
show
Documentation introduced by
Should the type for parameter $locale not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
60
     *
61
     * @return TranslationInterface
62
     */
63
    public function getTranslation($locale = null)
64
    {
65
        $locale = $locale ?: $this->currentLocale;
66
        if (null === $locale) {
67
            throw new \RuntimeException('No locale has been set and current locale is undefined.');
68
        }
69
70
        if (isset($this->translationsCache[$locale])) {
71
            return $this->translationsCache[$locale];
72
        }
73
74
        $translation = $this->translations->get($locale);
75
        if (null !== $translation) {
76
            $this->translationsCache[$locale] = $translation;
77
78
            return $translation;
79
        }
80
81
        if ($locale !== $this->fallbackLocale) {
82
            if (isset($this->translationsCache[$this->fallbackLocale])) {
83
                return $this->translationsCache[$this->fallbackLocale];
84
            }
85
86
            $fallbackTranslation = $this->translations->get($this->fallbackLocale);
87
            if (null !== $fallbackTranslation) {
88
                $this->translationsCache[$this->fallbackLocale] = $fallbackTranslation;
89
90
                return $fallbackTranslation;
91
            }
92
        }
93
94
        $translation = $this->createTranslation();
95
        $translation->setLocale($locale);
96
97
        $this->addTranslation($translation);
98
99
        $this->translationsCache[$locale] = $translation;
100
101
        return $translation;
102
    }
103
104
    /**
105
     * @return TranslationInterface[]
106
     */
107
    public function getTranslations()
108
    {
109
        return $this->translations;
110
    }
111
112
    /**
113
     * @param TranslationInterface $translation
114
     *
115
     * @return bool
116
     */
117
    public function hasTranslation(TranslationInterface $translation)
118
    {
119
        return isset($this->translationsCache[$translation->getLocale()]) || $this->translations->containsKey($translation->getLocale());
120
    }
121
122
    /**
123
     * @param TranslationInterface $translation
124
     */
125
    public function addTranslation(TranslationInterface $translation)
126
    {
127
        if (!$this->hasTranslation($translation)) {
128
            $this->translationsCache[$translation->getLocale()] = $translation;
129
130
            $this->translations->set($translation->getLocale(), $translation);
131
            $translation->setTranslatable($this);
132
        }
133
    }
134
135
    /**
136
     * @param TranslationInterface $translation
137
     */
138
    public function removeTranslation(TranslationInterface $translation)
139
    {
140
        if ($this->translations->removeElement($translation)) {
141
            unset($this->translationsCache[$translation->getLocale()]);
142
143
            $translation->setTranslatable(null);
144
        }
145
    }
146
147
    /**
148
     * @param string $currentLocale
149
     */
150
    public function setCurrentLocale($currentLocale)
151
    {
152
        $this->currentLocale = $currentLocale;
153
    }
154
155
    /**
156
     * @param string $fallbackLocale
157
     */
158
    public function setFallbackLocale($fallbackLocale)
159
    {
160
        $this->fallbackLocale = $fallbackLocale;
161
    }
162
163
    /**
164
     * Create resource translation model.
165
     *
166
     * @return TranslationInterface
167
     */
168
    abstract protected function createTranslation();
169
}
170