Passed
Push — develop ( c0a645...f538e8 )
by Daniel
05:28
created

ComponentLocation::getDynamicPageClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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