Test Failed
Push — release/3.x ( 447600...4272fa )
by
unknown
02:22 queued 10s
created

StaticReference::__construct()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 2
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Rik van der Kemp <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\UrlBundle\Entity;
8
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\ORM\Mapping as ORM;
11
12
/**
13
 * StaticReference
14
 *
15
 * @ORM\Table(name="static_reference")
16
 * @ORM\Entity(repositoryClass="Zicht\Bundle\UrlBundle\Entity\Repository\StaticReferenceRepository")
17
 * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
18
 */
19
class StaticReference
20
{
21
    /**
22
     * @var integer
23
     *
24
     * @ORM\Column(name="id", type="integer")
25
     * @ORM\Id
26
     * @ORM\GeneratedValue(strategy="AUTO")
27
     */
28
    private $id;
29
30
    /**
31
     * @var string
32
     *
33
     * @ORM\Column(name="machine_name", type="string", length=255)
34
     */
35
    private $machine_name;
36
37
    /**
38
     * @var StaticReferenceTranslation[]
39
     * @ORM\OneToMany(
40
     *     targetEntity="Zicht\Bundle\UrlBundle\Entity\StaticReferenceTranslation",
41
     *     mappedBy="static_reference",
42
     *     cascade={"persist", "remove"},
43
     *     orphanRemoval=true
44
     * )
45
     */
46
    public $translations;
47
48
    /**
49
     * Default construction of entity
50
     *
51
     * @param string $machineName
52
     * @param array $translations
53
     */
54
    public function __construct($machineName = null, $translations = null)
55
    {
56
        $this->translations = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type Zicht\Bundle\UrlBundle\E...cReferenceTranslation[] of property $translations.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        if (null !== $machineName) {
58
            $this->setMachineName($machineName);
59
        }
60
61
        if (null !== $translations) {
62
            foreach ($translations as $language => $url) {
63
                $this->addTranslations(new StaticReferenceTranslation($language, $url));
64
            }
65
        }
66
    }
67
68
    /**
69
     * Get id
70
     *
71
     * @return integer
72
     */
73
    public function getId()
74
    {
75
        return $this->id;
76
    }
77
78
    /**
79
     * Set machine_name
80
     *
81
     * @param string $machineName
82
     *
83
     * @return StaticReference
84
     */
85
    public function setMachineName($machineName)
86
    {
87
        $this->machine_name = $machineName;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Get machine_name
94
     *
95
     * @return string
96
     */
97
    public function getMachineName()
98
    {
99
        return $this->machine_name;
100
    }
101
102
    /**
103
     * @param string $locale
104
     *
105
     * @return bool
106
     */
107
    public function getTranslation($locale)
108
    {
109
        return $this->hasTranslation($locale);
110
    }
111
112
    /**
113
     * Setter for translations
114
     *
115
     * @param mixed $translations
116
     * @return void
117
     */
118
    public function setTranslations($translations)
119
    {
120
        $this->translations = $translations;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function __toString()
127
    {
128
        if (!empty($this->machine_name)) {
129
            return $this->machine_name;
130
        } else {
131
            return (string)$this->id;
132
        }
133
    }
134
135
    /**
136
     * Checks if a translation is set for the given locale
137
     *
138
     * @param string $locale
139
     * @return bool
140
     */
141
    public function hasTranslation($locale)
142
    {
143
        foreach ($this->translations as $translation) {
144
            if ($locale == $translation->getLocale()) {
145
                return $translation;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $translation returns the type Zicht\Bundle\UrlBundle\E...ticReferenceTranslation which is incompatible with the documented return type boolean.
Loading history...
146
            }
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * @return mixed
154
     */
155
    public function getTranslations()
156
    {
157
        return $this->translations;
158
    }
159
160
    /**
161
     * Set translations that are not yet initialized
162
     *
163
     * @return void
164
     */
165
    public function addMissingTranslations()
166
    {
167
        foreach ($this->translations as $translation) {
168
            $translation->setStaticReference($this);
169
        }
170
    }
171
172
    /**
173
     * Add a translation
174
     *
175
     * @param StaticReferenceTranslation $translation
176
     * @return void
177
     */
178
    public function addTranslations(StaticReferenceTranslation $translation)
179
    {
180
        $translation->setStaticReference($this);
181
182
        $this->translations[] = $translation;
183
    }
184
}
185