Completed
Push — develop ( e326d5...b2cbdf )
by Daniel
09:21
created

AbstractContent::setComponentLocations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
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 Symfony\Component\Serializer\Annotation\MaxDepth;
13
14
/**
15
 * Class AbstractContent
16
 * @package Silverback\ApiComponentBundle\Entity
17
 * @author Daniel West <[email protected]>
18
 * @ORM\Entity()
19
 * @ORM\Table(name="content")
20
 * @ORM\InheritanceType("SINGLE_TABLE")
21
 * @ORM\DiscriminatorColumn(name="type", type="string")
22
 * @ORM\DiscriminatorMap({
23
 *     "component_group" = "Silverback\ApiComponentBundle\Entity\Content\ComponentGroup\ComponentGroup",
24
 *     "abstract_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\AbstractPage",
25
 *     "page" = "Silverback\ApiComponentBundle\Entity\Content\Page\Page",
26
 *     "dynamic_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\Dynamic\AbstractDynamicPage",
27
 *     "article_page" = "Silverback\ApiComponentBundle\Entity\Content\Page\Dynamic\ArticlePage"
28
 * })
29
 */
30
abstract class AbstractContent implements ContentInterface
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
     * @MaxDepth(40)
43
     * @var Collection|ComponentLocation[]
44
     */
45 7
    protected $componentLocations;
46
47 7
    public function __construct()
48 7
    {
49 7
        $this->id = Uuid::uuid4()->getHex();
50
        $this->componentLocations = new ArrayCollection;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getId(): string
57
    {
58
        return $this->id;
59
    }
60
61
    /**
62
     * @return Collection|ComponentLocation[]
63
     */
64
    public function getComponentLocations(): Collection
65
    {
66
        return $this->componentLocations;
67
    }
68
69
    /**
70
     * @param \Silverback\ApiComponentBundle\Entity\Component\ComponentLocation[]|iterable $componentLocations
71
     * @return AbstractContent
72
     */
73
    public function setComponentLocations(iterable $componentLocations): AbstractContent
74
    {
75
        $this->componentLocations = new ArrayCollection;
76
        /** @var ComponentLocation $componentLocation */
77
        foreach ($componentLocations as $componentLocation) {
78
            $this->addComponentLocation($componentLocation);
79
        }
80
        return $this;
81
    }
82
83
    /**
84
     * @param ComponentLocation $componentLocation
85
     * @return AbstractContent
86
     */
87
    public function addComponentLocation(ComponentLocation $componentLocation): AbstractContent
88
    {
89
        if (!$this->componentLocations->contains($componentLocation)) {
90
            $this->componentLocations->add($componentLocation);
91
            $componentLocation->setContent($this);
92
        }
93
        return $this;
94
    }
95
96
    /**
97
     * @param ComponentLocation $componentLocation
98
     * @return AbstractContent
99
     */
100
    public function removeComponentLocation(ComponentLocation $componentLocation): AbstractContent
101
    {
102
        if ($this->componentLocations->contains($componentLocation)) {
103
            $this->componentLocations->removeElement($componentLocation);
104
            if ($componentLocation->getContent() === $this) {
105
                $componentLocation->setContent(null);
106
            }
107
        }
108
        return $this;
109
    }
110
}
111