Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

ComponentLocation::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Entity\Content;
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\Component\AbstractComponent;
11
use Silverback\ApiComponentBundle\Entity\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\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="components")
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({"component", "content", "route"})
50
     * @var AbstractComponent
51
     */
52
    private $component;
53
54
    public static function loadValidatorMetadata(ClassMetadata $metadata)
55
    {
56
        $metadata->addPropertyConstraint(
57
            'content',
58
            new Assert\NotBlank()
59
        );
60
        $metadata->addPropertyConstraint(
61
            'component',
62
            new Assert\NotBlank()
63
        );
64
    }
65
66
    /**
67
     * ComponentLocation constructor.
68
     * @param null|AbstractContent $newContent
69
     * @param null|AbstractComponent $newComponent
70
     */
71
    public function __construct(
72
        ?AbstractContent $newContent = null,
73
        ?AbstractComponent $newComponent = null
74
    ) {
75
        $this->id = Uuid::uuid4()->getHex();
76
        if ($newContent) {
77
            $this->setContent($newContent);
78
        }
79
        if ($newComponent) {
80
            $this->setComponent($newComponent);
81
        }
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getId(): string
88
    {
89
        return $this->id;
90
    }
91
92
    /**
93
     * @return AbstractContent
94
     */
95
    public function getContent(): AbstractContent
96
    {
97
        return $this->content;
98
    }
99
100
    /**
101
     * @param AbstractContent $content
102
     * @param bool|null $sortLast
103
     */
104
    public function setContent(AbstractContent $content, ?bool $sortLast = true): void
105
    {
106
        $this->content = $content;
107
        if (null === $this->sort || $sortLast !== null) {
108
            $this->setSort($this->calculateSort($sortLast));
109
        }
110
    }
111
112
    /**
113
     * @return AbstractComponent
114
     */
115
    public function getComponent(): AbstractComponent
116
    {
117
        return $this->component;
118
    }
119
120
    /**
121
     * @param AbstractComponent $component
122
     */
123
    public function setComponent(AbstractComponent $component): void
124
    {
125
        $this->component = $component;
126
    }
127
128
    /**
129
     * @return Collection|AbstractFeatureItem[]
130
     */
131
    public function getSortCollection(): Collection
132
    {
133
        return $this->content ? $this->content->getComponents() : new ArrayCollection;
134
    }
135
}
136