Passed
Push — develop ( 9486de...e25dc5 )
by Daniel
06:06
created

ComponentLocation::getSortCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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()
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
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
        $this->content->addComponentLocation($this);
117 2
    }
118
119
    /**
120
     * @return AbstractComponent
121
     */
122 1
    public function getComponent(): AbstractComponent
123
    {
124 1
        return $this->component;
125
    }
126
127
    /**
128
     * @param AbstractComponent $component
129
     */
130 2
    public function setComponent(AbstractComponent $component): void
131
    {
132 2
        $this->component = $component;
133 2
    }
134
135
    /**
136
     * @return Collection|AbstractFeatureItem[]
137
     */
138 2
    public function getSortCollection(): Collection
139
    {
140 2
        return $this->content ? $this->content->getComponentLocations() : new ArrayCollection;
141
    }
142
143
    /**
144
     * @return null|string
145
     */
146 1
    public function getDynamicPageClass(): ?string
147
    {
148 1
        return $this->dynamicPageClass;
149
    }
150
151
    /**
152
     * @param null|string $dynamicPageClass
153
     */
154 1
    public function setDynamicPageClass(?string $dynamicPageClass): void
155
    {
156 1
        $this->dynamicPageClass = $dynamicPageClass;
157 1
    }
158
}
159