Completed
Push — master ( 0be8e1...585ca2 )
by Daniel
59:26 queued 46:02
created

AbstractContent::getComponentLocations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Entity\Content;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Ramsey\Uuid\Uuid;
11
use Silverback\ApiComponentBundle\Entity\Component\ComponentLocation;
12
use Silverback\ApiComponentBundle\Entity\TimestampedEntityTrait;
13
14
/**
15
 * @package Silverback\ApiComponentBundle\Entity
16
 * @author Daniel West <[email protected]>
17
 * @ORM\Entity(repositoryClass="Silverback\ApiComponentBundle\Repository\Content\AbstractContentRepository")
18
 * @ORM\Table(name="content")
19
 * @ORM\InheritanceType("SINGLE_TABLE")
20
 * @ORM\DiscriminatorColumn(name="type", type="string")
21
 * @ORM\DiscriminatorMap({
22
 *     "abstract_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\AbstractPage",
23
 *     "static_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\StaticPage",
24
 *     "dynamic_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\DynamicPage",
25
 *     "component_group" = "Silverback\ApiComponentBundle\Entity\Content\ComponentGroup\ComponentGroup"
26
 * })
27
 */
28
abstract class AbstractContent implements ContentInterface
29
{
30
    use TimestampedEntityTrait;
31
32
    /**
33
     * @ORM\Id()
34
     * @ORM\Column(type="string")
35
     * @var string
36
     */
37
    protected $id;
38
39
    /**
40
     * @ORM\OneToMany(targetEntity="Silverback\ApiComponentBundle\Entity\Component\ComponentLocation", mappedBy="content", cascade={"persist", "remove"})
41
     * @ORM\OrderBy({"sort"="ASC"})
42
     * @var Collection|ComponentLocation[]
43
     */
44
    protected $componentLocations;
45
46
    public function __construct()
47 7
    {
48
        $this->id = Uuid::uuid4()->getHex();
49 7
        $this->componentLocations = new ArrayCollection;
50 7
    }
51 7
52
    /**
53
     * @return string
54
     */
55
    public function getId(): string
56
    {
57
        return $this->id;
58
    }
59
60
    /**
61
     * @return Collection|ComponentLocation[]
62
     */
63
    public function getComponentLocations(): Collection
64
    {
65
        return $this->componentLocations;
66
    }
67
68
    /**
69
     * @param \Silverback\ApiComponentBundle\Entity\Component\ComponentLocation[]|iterable $componentLocations
70
     * @return AbstractContent
71
     */
72
    public function setComponentLocations(iterable $componentLocations): AbstractContent
73
    {
74
        $this->componentLocations = new ArrayCollection;
75
        /** @var ComponentLocation $componentLocation */
76
        foreach ($componentLocations as $componentLocation) {
77
            $this->addComponentLocation($componentLocation);
78
        }
79
        return $this;
80
    }
81
82
    /**
83
     * @param ComponentLocation $componentLocation
84
     * @return AbstractContent
85
     */
86
    public function addComponentLocation(ComponentLocation $componentLocation): AbstractContent
87
    {
88
        if (!$this->componentLocations->contains($componentLocation)) {
89
            $this->componentLocations->add($componentLocation);
90
            $componentLocation->setContent($this);
91
        }
92
        return $this;
93
    }
94
95
    /**
96
     * @param ComponentLocation $componentLocation
97
     * @return AbstractContent
98
     */
99
    public function removeComponentLocation(ComponentLocation $componentLocation): AbstractContent
100
    {
101
        if ($this->componentLocations->contains($componentLocation)) {
102
            $this->componentLocations->removeElement($componentLocation);
103
            if ($componentLocation->getContent() === $this) {
104
                $componentLocation->setContent(null);
105
            }
106
        }
107
        return $this;
108
    }
109
}
110