PageI18nTrait::addTranslation()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 12
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 29
ccs 0
cts 13
cp 0
crap 90
rs 8.0555
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
9
trait PageI18nTrait
10
{
11
    /**
12
     * //rfc5646.
13
     *
14
     * @ORM\Column(type="string", length=5, nullable=true)
15
     */
16
    protected $locale;
17
18 3
    public function __constructI18n()
19
    {
20 3
        $this->translations = new ArrayCollection();
21 3
    }
22
23
    /**
24
     * @ORM\ManyToMany(targetEntity="PiedWeb\CMSBundle\Entity\PageInterface")
25
     */
26
    protected $translations;
27
28
    public function getLocale(): ?string
29
    {
30
        return $this->locale;
31
    }
32
33
    public function setLocale($locale): self
34
    {
35
        $this->locale = $locale;
36
37
        return $this;
38
    }
39
40
    public function getTranslations(): Collection
41
    {
42
        return $this->translations;
43
    }
44
45
    public function addTranslation(self $translation, $recursive = true): self
46
    {
47
        if (! $this->translations->contains($translation) && $this != $translation) {
48
            $this->translations[] = $translation;
49
        }
50
51
        // Add the other ('ever exist') translations to the new added Translation
52
        if (true === $recursive) {
53
            foreach ($this->translations as $otherTranslation) {
54
                $translation->addTranslation($otherTranslation, false);
55
            }
56
        }
57
58
        // Reversing the syncing
59
        // Add this Page to the translated Page
60
        // + Add the translated Page to the other translation
61
        if (true === $recursive) {
62
            $translation->addTranslation($this, false);
63
64
            foreach ($this->translations as $otherTranslation) {
65
                if ($otherTranslation != $this // déjà fait
66
                    && $otherTranslation != $translation // on ne se référence pas soit-même
67
                ) {
68
                    $otherTranslation->addTranslation($translation, false);
69
                }
70
            }
71
        }
72
73
        return $this;
74
    }
75
76
    public function removeTranslation(self $translation, $recursive = true): self
77
    {
78
        if ($this->translations->contains($translation)) {
79
            $this->translations->removeElement($translation);
80
81
            if (true === $recursive) {
82
                foreach ($this->translations as $otherTranslation) {
83
                    $translation->removeTranslation($otherTranslation, false);
84
                }
85
            }
86
        }
87
88
        if (true === $recursive) {
89
            $translation->removeTranslation($this, false);
90
91
            foreach ($this->translations as $otherTranslation) {
92
                if ($otherTranslation != $this // déjà fait
93
                    && $otherTranslation != $translation // on ne se déréférence pas soit-même
94
                ) {
95
                    $otherTranslation->removeTranslation($translation, false);
96
                }
97
            }
98
        }
99
100
        return $this;
101
    }
102
}
103