Test Failed
Push — develop ( 92c10d...ff12cf )
by Daniel
05:05
created

ComponentLocation::setDynamicPageClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Entity\Component;
6
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Ramsey\Uuid\Uuid;
10
use Silverback\ApiComponentBundle\Entity\Component\Feature\AbstractFeatureItem;
11
use Silverback\ApiComponentBundle\Entity\Content\AbstractContent;
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\Context\ExecutionContextInterface;
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\Component\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", "component"})
50
     * @var AbstractComponent
51
     */
52
    private $component;
53
54
    public static function loadValidatorMetadata(ClassMetadata $metadata): void
55
    {
56
        $metadata->addPropertyConstraint(
57
            'component',
58
            new Assert\NotBlank()
59
        );
60 1
    }
61
62 1
    /**
63 1
     * ComponentLocation constructor.
64 1
     * @param null|AbstractContent $newContent
65
     * @param null|AbstractComponent $newComponent
66 1
     */
67 1
    public function __construct(
68 1
        ?AbstractContent $newContent = null,
69
        ?AbstractComponent $newComponent = null
70 1
    ) {
71
        $this->id = Uuid::uuid4()->getHex();
72
        if ($newContent) {
73
            $this->setContent($newContent);
74
        }
75
        if ($newComponent) {
76
            $this->setComponent($newComponent);
77 4
        }
78
    }
79
80
    /**
81 4
     * @return string
82 4
     */
83
    public function getId(): string
84
    {
85 4
        return $this->id;
86
    }
87
88 4
    /**
89
     * @return AbstractContent|null
90
     */
91
    public function getContent(): ?AbstractContent
92
    {
93
        return $this->content;
94
    }
95
96
    public function setContent(?AbstractContent $content, ?bool $sortLast = true): self
97
    {
98
        if ($content !== $this->content) {
99
            $this->content = $content;
100
            if (null === $this->sort || $sortLast !== null) {
101
                $this->setSort($this->calculateSort($sortLast));
102
            }
103
            if ($content) {
104
                $content->addComponentLocation($this);
105
            }
106
        }
107
        return $this;
108
    }
109
110
    /**
111
     * @return AbstractComponent
112
     */
113
    public function getComponent(): AbstractComponent
114
    {
115
        return $this->component;
116
    }
117
118
    public function setComponent(AbstractComponent $component): self
119
    {
120
        $this->component = $component;
121
        return $this;
122
    }
123
124
    /**
125
     * @return Collection|AbstractFeatureItem[]|null
126
     */
127
    public function getSortCollection(): ?Collection
128
    {
129
        return $this->getContent() ? $this->getContent()->getComponentLocations() : null;
130
    }
131
132
    public function __toString()
133
    {
134
        $content = $this->getContent();
135
        return sprintf('location/%s/%s', $content ? get_class($content) : 'no-content', get_class($this->getComponent()));
0 ignored issues
show
introduced by
$content is of type Silverback\ApiComponentB...Content\AbstractContent, thus it always evaluated to true.
Loading history...
136
    }
137
}
138