Passed
Push — develop ( 9af182...91a680 )
by Daniel
05:16 queued 26s
created

ComponentLocation::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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