Completed
Pull Request — 5.4 (#2626)
by
unknown
06:14
created

NodeTranslation::getSlugPart()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Kunstmaan\NodeBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\Mapping as ORM;
8
use Kunstmaan\AdminBundle\Entity\AbstractEntity;
9
use Kunstmaan\NodeBundle\Form\NodeTranslationAdminType;
10
use Symfony\Component\Validator\Constraints as Assert;
11
12
/**
13
 * NodeTranslation
14
 *
15
 * @ORM\Entity(repositoryClass="Kunstmaan\NodeBundle\Repository\NodeTranslationRepository")
16
 * @ORM\Table(
17
 *     name="kuma_node_translations",
18
 *     uniqueConstraints={@ORM\UniqueConstraint(name="ix_kuma_node_translations_node_lang", columns={"node_id", "lang"})},
19
 *     indexes={@ORM\Index(name="idx__node_translation_lang_url", columns={"lang", "url"})}
20
 * )
21
 * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
22
 */
23
class NodeTranslation extends AbstractEntity
24
{
25
    /**
26
     * @var Node
27
     *
28
     * @ORM\ManyToOne(targetEntity="Node", inversedBy="nodeTranslations")
29
     * @ORM\JoinColumn(name="node_id", referencedColumnName="id")
30
     */
31
    protected $node;
32
33
    /**
34
     * @var string
35
     *
36
     * @ORM\Column(type="string")
37
     */
38
    protected $lang;
39
40
    /**
41
     * @var bool
42
     *
43
     * @ORM\Column(type="boolean")
44
     */
45
    protected $online = false;
46
47
    /**
48
     * @var string
49
     *
50
     * @ORM\Column(type="string")
51
     */
52
    protected $title;
53
54
    /**
55
     * @var string
56
     *
57
     * @ORM\Column(type="string", nullable=true)
58
     * @Assert\Regex("/^[a-zA-Z0-9\-_\/]+$/")
59
     */
60
    protected $slug;
61
62
    /**
63
     * @var string
64
     *
65
     * @ORM\Column(type="string", nullable=true)
66
     */
67
    protected $url;
68
69
    /**
70
     * @var NodeVersion
71
     *
72
     * @ORM\ManyToOne(targetEntity="NodeVersion")
73
     * @ORM\JoinColumn(name="public_node_version_id", referencedColumnName="id")
74
     */
75
    protected $publicNodeVersion;
76
77
    /**
78
     * @var ArrayCollection
79
     * @Assert\Valid()
80
     * @ORM\OneToMany(targetEntity="NodeVersion", mappedBy="nodeTranslation")
81
     * @ORM\OrderBy({"created" = "ASC"})
82
     */
83
    protected $nodeVersions;
84
85
    /**
86
     * @var int
87
     *
88
     * @ORM\Column(type="smallint", nullable=true)
89
     */
90
    protected $weight;
91
92
    /**
93
     * @var \DateTime
94
     *
95
     * @ORM\Column(type="datetime", nullable=true)
96
     */
97
    protected $created;
98
99
    /**
100
     * @var \DateTime
101
     *
102
     * @ORM\Column(type="datetime", nullable=true)
103
     */
104
    protected $updated;
105
106
    /**
107
     * contructor
108
     */
109 59
    public function __construct()
110
    {
111 59
        $this->nodeVersions = new ArrayCollection();
112 59
        $this->setCreated(new \DateTime());
113 59
        $this->setUpdated(new \DateTime());
114 59
    }
115
116
    /**
117
     * Set node
118
     *
119
     * @param Node $node
120
     *
121
     * @return NodeTranslation
122
     */
123 30
    public function setNode($node)
124
    {
125 30
        $this->node = $node;
126
127 30
        return $this;
128
    }
129
130
    /**
131
     * Get Node
132
     *
133
     * @return Node
134
     */
135 14
    public function getNode()
136
    {
137 14
        return $this->node;
138
    }
139
140
    /**
141
     * Set lang
142
     *
143
     * @param string $lang
144
     *
145
     * @return NodeTranslation
146
     */
147 17
    public function setLang($lang)
148
    {
149 17
        $this->lang = $lang;
150
151 17
        return $this;
152
    }
153
154
    /**
155
     * Get lang
156
     *
157
     * @return string
158
     */
159 13
    public function getLang()
160
    {
161 13
        return $this->lang;
162
    }
163
164
    /**
165
     * Is online
166
     *
167
     * @return bool
168
     */
169 8
    public function isOnline()
170
    {
171 8
        return $this->online;
172
    }
173
174
    /**
175
     * Set online
176
     *
177
     * @param bool $online
178
     *
179
     * @return NodeTranslation
180
     */
181 13
    public function setOnline($online)
182
    {
183 13
        $this->online = $online;
184
185 13
        return $this;
186
    }
187
188
    /**
189
     * Set title
190
     *
191
     * @param string $title
192
     *
193
     * @return NodeTranslation
194
     */
195 10
    public function setTitle($title)
196
    {
197 10
        $this->title = $title;
198
199 10
        return $this;
200
    }
201
202
    /**
203
     * Get title
204
     *
205
     * @return string
206
     */
207 3
    public function getTitle()
208
    {
209 3
        return $this->title;
210
    }
211
212
    /**
213
     * Set slug
214
     *
215
     * @param string $slug
216
     *
217
     * @return NodeTranslation
218
     */
219 3
    public function setSlug($slug)
220
    {
221 3
        $this->slug = $slug;
222
223 3
        return $this;
224
    }
225
226
    /**
227
     * Get slug
228
     *
229
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
230
     */
231 2
    public function getFullSlug()
232
    {
233 2
        $slug = $this->getSlugPart();
234
235 2
        if (empty($slug)) {
236 1
            return null;
237
        }
238
239 1
        return $slug;
240
    }
241
242
    /**
243
     * @return string
244
     */
245 2
    public function getSlugPart()
246
    {
247 2
        $slug = '';
248 2
        $parentNode = $this->getNode()->getParent();
249 2
        if ($parentNode !== null) {
250 1
            $nodeTranslation = $parentNode->getNodeTranslation($this->lang, true);
251
252 1
            if ($nodeTranslation !== null) {
253 1
                $parentSlug = $nodeTranslation->getSlugPart();
254 1
                if (!empty($parentSlug)) {
255 1
                    $slug = rtrim($parentSlug, '/') . '/';
256
                }
257
            }
258
        }
259 2
        $slug .= $this->getSlug();
260
261 2
        return $slug;
262
    }
263
264
    /**
265
     * Get slug
266
     *
267
     * @return string
268
     */
269 3
    public function getSlug()
270
    {
271 3
        return $this->slug;
272
    }
273
274
    /**
275
     * @param NodeVersion $publicNodeVersion
276
     *
277
     * @return NodeTranslation
278
     */
279 6
    public function setPublicNodeVersion(NodeVersion $publicNodeVersion)
280
    {
281 6
        $this->publicNodeVersion = $publicNodeVersion;
282
283 6
        return $this;
284
    }
285
286
    /**
287
     * @return NodeVersion
288
     */
289 11
    public function getPublicNodeVersion()
290
    {
291 11
        return $this->publicNodeVersion;
292
    }
293
294
    /**
295
     * @return NodeVersion
0 ignored issues
show
Documentation introduced by
Should the return type not be NodeVersion|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
296
     */
297 1
    public function getDraftNodeVersion()
298
    {
299 1
        return $this->getNodeVersion('draft');
300
    }
301
302
    /**
303
     * @return ArrayCollection
304
     */
305 4
    public function getNodeVersions()
306
    {
307 4
        return $this->nodeVersions;
308
    }
309
310
    /**
311
     * @param ArrayCollection $nodeVersions
312
     *
313
     * @return NodeTranslation
314
     */
315 2
    public function setNodeVersions(ArrayCollection $nodeVersions)
316
    {
317 2
        $this->nodeVersions = $nodeVersions;
318
319 2
        return $this;
320
    }
321
322
    /**
323
     * @param string $type
324
     *
325
     * @return NodeVersion|null
326
     */
327 4
    public function getNodeVersion($type)
328
    {
329 4
        if ($type == 'public') {
330 2
            return $this->publicNodeVersion;
331
        }
332
333 3
        $nodeVersions = $this->getNodeVersions();
334
335 3
        $max = \count($nodeVersions);
336 3
        for ($i = $max - 1; $i >= 0; --$i) {
337
            /* @var NodeVersion $nodeVersion */
338 2
            $nodeVersion = $nodeVersions[$i];
339
340 2
            if ($type == $nodeVersion->getType()) {
341 2
                return $nodeVersion;
342
            }
343
        }
344
345 1
        return null;
346
    }
347
348
    /**
349
     * Add nodeVersion
350
     *
351
     * @param NodeVersion $nodeVersion
352
     *
353
     * @return NodeTranslation
354
     */
355 11
    public function addNodeVersion(NodeVersion $nodeVersion)
356
    {
357 11
        $this->nodeVersions[] = $nodeVersion;
358 11
        $nodeVersion->setNodeTranslation($this);
359
360 11
        if ($nodeVersion->getType() == 'public') {
361 10
            $this->publicNodeVersion = $nodeVersion;
362
        }
363
364 11
        return $this;
365
    }
366
367
    /**
368
     * @return string
369
     */
370 1
    public function getDefaultAdminType()
371
    {
372 1
        return NodeTranslationAdminType::class;
373
    }
374
375
    /**
376
     * @param EntityManagerInterface $em   The entity manager
377
     * @param string                 $type The type
378
     *
379
     * @return object|null
380
     */
381 1
    public function getRef(EntityManagerInterface $em, $type = 'public')
382
    {
383 1
        $nodeVersion = $this->getNodeVersion($type);
384 1
385 1
        if ($nodeVersion instanceof NodeVersion) {
386
            return $this->getRefByNodeVersion($em, $nodeVersion);
387
        }
388 1
389
        return null;
390
    }
391
392
    /**
393
     * @param EntityManagerInterface $em   The entity manager
394
     * @param NodeVersion            $nodeVersion
395
     *
396 2
     * @return object|null
397
     */
398 2
    public function getRefByNodeVersion(EntityManagerInterface $em, NodeVersion $nodeVersion) {
399
        return $em->getRepository($nodeVersion->getRefEntityName())->find($nodeVersion->getRefId());
400 2
    }
401
402
    /**
403
     * @param string $url
404
     *
405
     * @return NodeTranslation
406 7
     */
407
    public function setUrl($url)
408 7
    {
409
        $this->url = $url;
410
411
        return $this;
412
    }
413
414
    /**
415
     * @return string
416 2
     */
417
    public function getUrl()
418 2
    {
419
        return $this->url;
420 2
    }
421
422
    /**
423
     * @param int $weight
424
     *
425
     * @return NodeTranslation
426 2
     */
427
    public function setWeight($weight)
428 2
    {
429
        $this->weight = $weight;
430
431
        return $this;
432
    }
433
434 1
    /**
435
     * @return int
436 1
     */
437
    public function getWeight()
438
    {
439
        return $this->weight;
440
    }
441
442
    /**
443
     * @return \DateTime
444 59
     */
445
    public function getCreated()
446 59
    {
447
        return $this->created;
448 59
    }
449
450
    /**
451
     * @param \DateTime $created
452
     *
453
     * @return NodeTranslation
454 1
     */
455
    public function setCreated($created)
456 1
    {
457
        $this->created = $created;
458
459
        return $this;
460
    }
461
462
    /**
463
     * @return \DateTime
464 59
     */
465
    public function getUpdated()
466 59
    {
467
        return $this->updated;
468 59
    }
469
470
    /**
471
     * @param \DateTime $updated
472
     *
473
     * @return NodeTranslation
474
     */
475
    public function setUpdated($updated)
476
    {
477
        $this->updated = $updated;
478
479
        return $this;
480
    }
481
}
482