Completed
Push — master ( 477095...a85fc8 )
by Daniel
05:52
created

AbstractContent::removeComponentLocation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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