Test Failed
Push — develop ( b13210...c32797 )
by Daniel
04:26
created

ComponentLocation::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Entity\Component;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Ramsey\Uuid\Uuid;
9
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent;
10
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent;
11
use Silverback\ApiComponentBundle\Entity\Component\Feature\AbstractFeatureItem;
12
use Silverback\ApiComponentBundle\Entity\SortableInterface;
13
use Silverback\ApiComponentBundle\Entity\SortableTrait;
14
use Silverback\ApiComponentBundle\Validator\Constraints as ACBAssert;
15
use Symfony\Component\Serializer\Annotation\Groups;
16
use Symfony\Component\Validator\Constraints as Assert;
17
use Symfony\Component\Validator\Mapping\ClassMetadata;
18
19
/**
20
 * Class ComponentLocation
21
 * @package Silverback\ApiComponentBundle\Entity\Component
22
 * @ACBAssert\ComponentLocation()
23
 * @ORM\Entity(repositoryClass="Silverback\ApiComponentBundle\Repository\ComponentLocationRepository")
24
 * @ORM\InheritanceType("SINGLE_TABLE")
25
 */
26
class ComponentLocation implements SortableInterface
27
{
28
    use SortableTrait;
29
30
    /**
31
     * @ORM\Id
32
     * @ORM\Column(type="string")
33
     * @var string
34
     */
35
    private $id;
36
37
    /**
38
     * @ORM\ManyToOne(targetEntity="Silverback\ApiComponentBundle\Entity\Content\AbstractContent", inversedBy="componentLocations")
39
     * @ORM\JoinColumn(onDelete="CASCADE")
40
     * @Groups({"component"})
41
     * @var AbstractContent
42
     */
43
    private $content;
44
45
    /**
46
     * @ORM\ManyToOne(targetEntity="Silverback\ApiComponentBundle\Entity\Component\AbstractComponent", inversedBy="locations")
47
     * @ORM\JoinColumn(onDelete="CASCADE")
48
     * @Groups({"default"})
49
     * @var AbstractComponent
50
     */
51
    private $component;
52
53
    /**
54
     * @ORM\Column(type="string", nullable=true)
55
     * @var null|string
56
     */
57
    protected $dynamicPageClass;
58
59
    public static function loadValidatorMetadata(ClassMetadata $metadata)
60
    {
61
        $metadata->addPropertyConstraint(
62
            'content',
63
            new Assert\NotBlank()
64
        );
65
        $metadata->addPropertyConstraint(
66
            'component',
67
            new Assert\NotBlank()
68
        );
69
    }
70
71
    /**
72
     * ComponentLocation constructor.
73
     * @param null|AbstractContent $newContent
74
     * @param null|AbstractComponent $newComponent
75
     */
76
    public function __construct(
77
        ?AbstractContent $newContent = null,
78
        ?AbstractComponent $newComponent = null
79
    ) {
80
        $this->id = Uuid::uuid4()->getHex();
81
        if ($newContent) {
82
            $this->setContent($newContent);
83
        }
84
        if ($newComponent) {
85
            $this->setComponent($newComponent);
86
        }
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getId(): string
93
    {
94
        return $this->id;
95
    }
96
97
    /**
98
     * @return AbstractContent|null
99
     */
100
    public function getContent(): ?AbstractContent
101
    {
102
        return $this->content;
103
    }
104
105
    /**
106
     * @param AbstractContent $content
107
     * @param bool|null $sortLast
108
     */
109
    public function setContent(?AbstractContent $content, ?bool $sortLast = true): void
110
    {
111
        $this->content = $content;
112
        if (null === $this->sort || $sortLast !== null) {
113
            $this->setSort($this->calculateSort($sortLast));
114
        }
115
        if ($this->content) {
116
            $this->content->addComponentLocation($this);
117
        }
118
    }
119
120
    /**
121
     * @return AbstractComponent
122
     */
123
    public function getComponent(): AbstractComponent
124
    {
125
        return $this->component;
126
    }
127
128
    /**
129
     * @param AbstractComponent $component
130
     */
131
    public function setComponent(AbstractComponent $component): void
132
    {
133
        $this->component = $component;
134
    }
135
136
    /**
137
     * @return Collection|AbstractFeatureItem[]
138
     */
139
    public function getSortCollection(): Collection
140
    {
141
        return $this->content ? $this->content->getComponentLocations() : new ArrayCollection;
142
    }
143
144
    /**
145
     * @return null|string
146
     */
147
    public function getDynamicPageClass(): ?string
148
    {
149
        return $this->dynamicPageClass;
150
    }
151
152
    /**
153
     * @param null|string $dynamicPageClass
154
     */
155
    public function setDynamicPageClass(?string $dynamicPageClass): void
156
    {
157
        $this->dynamicPageClass = $dynamicPageClass;
158
    }
159
}
160