Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

PageI18nTrait::addTranslation()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 12
nc 8
nop 2
dl 0
loc 29
rs 8.0555
c 1
b 0
f 0
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
    public function __constructI18n()
19
    {
20
        $this->translations = new ArrayCollection();
21
    }
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
    /**
41
     * @return Collection|self[]
42
     */
43
    public function getTranslations(): Collection
44
    {
45
        return $this->translations;
46
    }
47
48
    public function addTranslation(self $translation, $recursive = true): self
49
    {
50
        if (!$this->translations->contains($translation) && $this != $translation) {
51
            $this->translations[] = $translation;
52
        }
53
54
        // Add the other ('ever exist') translations to the new added Translation
55
        if (true === $recursive) {
56
            foreach ($this->translations as $otherTranslation) {
57
                $translation->addTranslation($otherTranslation, false);
58
            }
59
        }
60
61
        // Reversing the syncing
62
        // Add this Page to the translated Page
63
        // + Add the translated Page to the other translation
64
        if (true === $recursive) {
65
            $translation->addTranslation($this, false);
66
67
            foreach ($this->translations as $otherTranslation) {
68
                if ($otherTranslation != $this // déjà fait
69
                    && $otherTranslation != $translation // on ne se référence pas soit-même
70
                ) {
71
                    $otherTranslation->addTranslation($translation, false);
72
                }
73
            }
74
        }
75
76
        return $this;
77
    }
78
79
    public function removeTranslation(self $translation, $recursive = true): self
80
    {
81
        if ($this->translations->contains($translation)) {
82
            $this->translations->removeElement($translation);
83
84
            if (true === $recursive) {
85
                foreach ($this->translations as $otherTranslation) {
86
                    $translation->removeTranslation($otherTranslation, false);
87
                }
88
            }
89
        }
90
91
        if (true === $recursive) {
92
            $translation->removeTranslation($this, false);
93
94
            foreach ($this->translations as $otherTranslation) {
95
                if ($otherTranslation != $this // déjà fait
96
                    && $otherTranslation != $translation // on ne se déréférence pas soit-même
97
                ) {
98
                    $otherTranslation->removeTranslation($translation, false);
99
                }
100
            }
101
        }
102
103
        return $this;
104
    }
105
}
106