Completed
Push — translation-class ( c4acc3 )
by Kamil
18:30
created

TranslatableTrait::addTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 4
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
namespace Sylius\Component\Resource\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
16
/**
17
 * @author Gonzalo Vilaseca <[email protected]>
18
 */
19
trait TranslatableTrait
20
{
21
    /**
22
     * @var TranslationInterface[]
23
     */
24
    protected $translations;
25
26
    /**
27
     * @var string
28
     */
29
    protected $currentLocale;
30
31
    /**
32
     * Cache current translation. Useful in Doctrine 2.4+
33
     *
34
     * @var TranslationInterface
35
     */
36
    protected $currentTranslation;
37
38
    /**
39
     * @var string
40
     */
41
    protected $fallbackLocale;
42
43
    public function __construct()
44
    {
45
        $this->translations = new ArrayCollection();
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getTranslation($locale = null)
52
    {
53
        $locale = $locale ?: $this->currentLocale;
54
        if (null === $locale) {
55
            throw new \RuntimeException('No locale has been set and current locale is undefined.');
56
        }
57
58
        if ($this->currentTranslation && $locale === $this->currentTranslation->getLocale()) {
59
            return $this->currentTranslation;
60
        }
61
62
        $translation = $this->translations->get($locale)
63
        if (null === $translation) {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_IF
Loading history...
64
            if (null === $this->fallbackLocale) {
65
                throw new \RuntimeException('No fallback locale has been set.');
66
            }
67
68
            $fallbackTranslation = $this->translations->get($this->fallbackLocale)
69
            if (null === $fallbackTranslation) {
70
                $translation = $this->createTranslation();
71
                $translation->setLocale($locale);
72
73
                $this->addTranslation($translation);
74
            } else {
75
                $translation = clone $fallbackTranslation;
76
            }
77
        }
78
79
        $this->currentTranslation = $translation;
80
81
        return $translation;
82
    }
83
84
    /**
85
     * @return TranslationInterface[]
86
     */
87
    public function getTranslations()
88
    {
89
        return $this->translations;
90
    }
91
92
    /**
93
     * @param TranslationInterface $translation
94
     *
95
     * @return bool
96
     */
97
    public function hasTranslation(TranslationInterface $translation)
98
    {
99
        return $this->translations->containsKey($translation->getLocale());
100
    }
101
102
    /**
103
     * @param TranslationInterface $translation
104
     */
105
    public function addTranslation(TranslationInterface $translation)
106
    {
107
        if (!$this->translations->containsKey($translation->getLocale())) {
108
            $this->translations->set($translation->getLocale(), $translation);
109
            $translation->setTranslatable($this);
110
        }
111
    }
112
113
    /**
114
     * @param TranslationInterface $translation
115
     */
116
    public function removeTranslation(TranslationInterface $translation)
117
    {
118
        if ($this->translations->removeElement($translation)) {
119
            $translation->setTranslatable(null);
120
        }
121
    }
122
123
    /**
124
     * @param string $currentLocale
125
     */
126
    public function setCurrentLocale($currentLocale)
127
    {
128
        $this->currentLocale = $currentLocale;
129
    }
130
131
    /**
132
     * @param string $fallbackLocale
133
     */
134
    public function setFallbackLocale($fallbackLocale)
135
    {
136
        $this->fallbackLocale = $fallbackLocale;
137
    }
138
139
    /**
140
     * @return TranslationInterface
141
     */
142
    abstract protected function createTranslation();
143
}
144