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

ComponentLocation::getSortCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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