Completed
Push — develop ( e72854...0832ef )
by Daniel
06:46
created

AbstractContent   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 80
ccs 4
cts 24
cp 0.1666
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A removeComponentLocation() 0 9 3
A getId() 0 3 1
A setComponentLocations() 0 8 2
A getComponentLocations() 0 3 1
A addComponentLocation() 0 7 2
A __construct() 0 4 1
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
 * 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
    use TimestampedEntityTrait;
33
34
    /**
35
     * @ORM\Id()
36
     * @ORM\Column(type="string")
37
     * @var string
38
     */
39
    protected $id;
40
41
    /**
42
     * @ORM\OneToMany(targetEntity="Silverback\ApiComponentBundle\Entity\Component\ComponentLocation", mappedBy="content", cascade={"persist", "remove"})
43
     * @ORM\OrderBy({"sort"="ASC"})
44
     * @var Collection|ComponentLocation[]
45
     */
46
    protected $componentLocations;
47 7
48
    public function __construct()
49 7
    {
50 7
        $this->id = Uuid::uuid4()->getHex();
51 7
        $this->componentLocations = new ArrayCollection;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getId(): string
58
    {
59
        return $this->id;
60
    }
61
62
    /**
63
     * @return Collection|ComponentLocation[]
64
     */
65
    public function getComponentLocations(): Collection
66
    {
67
        return $this->componentLocations;
68
    }
69
70
    /**
71
     * @param \Silverback\ApiComponentBundle\Entity\Component\ComponentLocation[]|iterable $componentLocations
72
     * @return AbstractContent
73
     */
74
    public function setComponentLocations(iterable $componentLocations): AbstractContent
75
    {
76
        $this->componentLocations = new ArrayCollection;
77
        /** @var ComponentLocation $componentLocation */
78
        foreach ($componentLocations as $componentLocation) {
79
            $this->addComponentLocation($componentLocation);
80
        }
81
        return $this;
82
    }
83
84
    /**
85
     * @param ComponentLocation $componentLocation
86
     * @return AbstractContent
87
     */
88
    public function addComponentLocation(ComponentLocation $componentLocation): AbstractContent
89
    {
90
        if (!$this->componentLocations->contains($componentLocation)) {
91
            $this->componentLocations->add($componentLocation);
92
            $componentLocation->setContent($this);
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * @param ComponentLocation $componentLocation
99
     * @return AbstractContent
100
     */
101
    public function removeComponentLocation(ComponentLocation $componentLocation): AbstractContent
102
    {
103
        if ($this->componentLocations->contains($componentLocation)) {
104
            $this->componentLocations->removeElement($componentLocation);
105
            if ($componentLocation->getContent() === $this) {
106
                $componentLocation->setContent(null);
107
            }
108
        }
109
        return $this;
110
    }
111
}
112