Completed
Push — develop ( e43479...d5109f )
by Daniel
07:00 queued 23s
created

ComponentLocation::setContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Entity\Content\Component;
4
5
use ApiPlatform\Core\Annotation\ApiResource;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Ramsey\Uuid\Uuid;
10
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent;
11
use Silverback\ApiComponentBundle\Entity\Content\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\Content\Component
22
 * @ApiResource()
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\Content\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
    protected $dynamicPageClass;
59
60 6
    public static function loadValidatorMetadata(ClassMetadata $metadata)
61
    {
62 6
        $metadata->addPropertyConstraint(
63 6
            'content',
64 6
            new Assert\NotBlank()
65
        );
66 6
        $metadata->addPropertyConstraint(
67 6
            'component',
68 6
            new Assert\NotBlank()
69
        );
70 6
    }
71
72
    /**
73
     * ComponentLocation constructor.
74
     * @param null|AbstractContent $newContent
75
     * @param null|AbstractComponent $newComponent
76
     */
77 6
    public function __construct(
78
        ?AbstractContent $newContent = null,
79
        ?AbstractComponent $newComponent = null
80
    ) {
81 6
        $this->id = Uuid::uuid4()->getHex();
82 6
        if ($newContent) {
83 1
            $this->setContent($newContent);
84
        }
85 6
        if ($newComponent) {
86 1
            $this->setComponent($newComponent);
87
        }
88 6
    }
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 2
    public function getContent(): ?AbstractContent
102
    {
103 2
        return $this->content;
104
    }
105
106
    /**
107
     * @param AbstractContent $content
108
     * @param bool|null $sortLast
109
     */
110 2
    public function setContent(?AbstractContent $content, ?bool $sortLast = true): void
111
    {
112 2
        $this->content = $content;
113 2
        if (null === $this->sort || $sortLast !== null) {
114 2
            $this->setSort($this->calculateSort($sortLast));
115
        }
116 2
        if ($this->content) {
117 2
            $this->content->addComponentLocation($this);
118
        }
119 2
    }
120
121
    /**
122
     * @return AbstractComponent
123
     */
124 1
    public function getComponent(): AbstractComponent
125
    {
126 1
        return $this->component;
127
    }
128
129
    /**
130
     * @param AbstractComponent $component
131
     */
132 2
    public function setComponent(AbstractComponent $component): void
133
    {
134 2
        $this->component = $component;
135 2
    }
136
137
    /**
138
     * @return Collection|AbstractFeatureItem[]
139
     */
140 2
    public function getSortCollection(): Collection
141
    {
142 2
        return $this->content ? $this->content->getComponentLocations() : new ArrayCollection;
143
    }
144
145
    /**
146
     * @return null|string
147
     */
148 1
    public function getDynamicPageClass(): ?string
149
    {
150 1
        return $this->dynamicPageClass;
151
    }
152
153
    /**
154
     * @param null|string $dynamicPageClass
155
     */
156 1
    public function setDynamicPageClass(?string $dynamicPageClass): void
157
    {
158 1
        $this->dynamicPageClass = $dynamicPageClass;
159 1
    }
160
}
161