Passed
Push — master ( e9e739...1e7f19 )
by Yannick
09:01
created

CShortcut   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 176
rs 10
c 0
b 0
f 0
wmc 19

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 11 2
A getType() 0 5 1
A getTool() 0 6 1
A setIcon() 0 5 1
A getResourceIdentifier() 0 3 1
A getResourceName() 0 3 1
A __toString() 0 3 1
A setShortCutNode() 0 5 1
A setCustomImageUrl() 0 5 1
A getUrlOverride() 0 3 1
A setTitle() 0 5 1
A getId() 0 3 1
A setResourceName() 0 3 1
A getShortCutNode() 0 3 1
A getIcon() 0 3 1
A setUrlOverride() 0 5 1
A getCustomImageUrl() 0 3 1
A getTitle() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Entity;
8
9
use Chamilo\CoreBundle\Entity\AbstractResource;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\ResourceNode;
12
use Chamilo\CourseBundle\Repository\CShortcutRepository;
13
use Doctrine\ORM\Mapping as ORM;
14
use Stringable;
15
use Symfony\Component\Serializer\Annotation\Groups;
16
use Symfony\Component\Validator\Constraints as Assert;
17
18
#[ORM\Table(name: 'c_shortcut')]
19
#[ORM\Entity(repositoryClass: CShortcutRepository::class)]
20
class CShortcut extends AbstractResource implements ResourceInterface, Stringable
21
{
22
    #[ORM\Column(name: 'id', type: 'integer')]
23
    #[ORM\Id]
24
    #[ORM\GeneratedValue]
25
    protected ?int $id = null;
26
27
    #[Assert\NotBlank]
28
    #[Groups(['cshortcut:read'])]
29
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
30
    protected string $title;
31
32
    #[ORM\OneToOne(targetEntity: ResourceNode::class, inversedBy: 'shortCut')]
33
    #[ORM\JoinColumn(name: 'shortcut_node_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
34
    protected ResourceNode $shortCutNode;
35
36
    #[Groups(['cshortcut:read'])]
37
    protected string $url;
38
39
    #[Groups(['cshortcut:read'])]
40
    protected string $tool;
41
42
    #[Groups(['cshortcut:read'])]
43
    protected string $type;
44
45
    #[Groups(['cshortcut:read'])]
46
    private ?string $customImageUrl = null;
47
48
    #[Groups(['cshortcut:read'])]
49
    public ?string $target = null;
50
51
    /**
52
     * Optional custom URL to override the default /r/.../link format.
53
     * Example (for blogs): /resources/blog/{courseNodeId}/{blogId}/posts?cid=...&sid=...&gid=0
54
     */
55
    #[Groups(['cshortcut:read'])]
56
    private ?string $urlOverride = null;
57
58
    /**
59
     * Optional icon name (e.g., 'mdi-notebook-outline' for CBlog).
60
     */
61
    #[Groups(['cshortcut:read'])]
62
    private ?string $icon = null;
63
64
    public function __toString(): string
65
    {
66
        return $this->getTitle();
67
    }
68
69
    public function getId(): int
70
    {
71
        return $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->id could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
72
    }
73
74
    public function getResourceIdentifier(): int
75
    {
76
        return $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->id could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
79
    public function getResourceName(): string
80
    {
81
        return $this->getTitle();
82
    }
83
84
    public function setResourceName(string $name): self
85
    {
86
        return $this->setTitle($name);
87
    }
88
89
    public function getTitle(): string
90
    {
91
        return $this->title;
92
    }
93
94
    public function setTitle(string $title): self
95
    {
96
        $this->title = $title;
97
98
        return $this;
99
    }
100
101
    public function getShortCutNode(): ResourceNode
102
    {
103
        return $this->shortCutNode;
104
    }
105
106
    public function setShortCutNode(ResourceNode $shortCutNode): self
107
    {
108
        $this->shortCutNode = $shortCutNode;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Main URL for the shortcut:
115
     * - If a custom URL was set (e.g., for CBlog), return that one.
116
     * - Otherwise, fallback to the legacy /r/{tool}/{type}/{nodeId}/link pattern.
117
     */
118
    public function getUrl(): string
119
    {
120
        if (!empty($this->urlOverride)) {
121
            return $this->urlOverride;
122
        }
123
124
        return '/r/'.
125
            $this->getShortCutNode()->getResourceType()->getTool()->getTitle().'/'.
126
            $this->getShortCutNode()->getResourceType()->getTitle().'/'.
127
            $this->getShortCutNode()->getId().
128
            '/link';
129
    }
130
131
    /**
132
     * Tool name (derived from the shortcut node).
133
     */
134
    public function getTool(): string
135
    {
136
        return $this->getShortCutNode()
137
            ->getResourceType()
138
            ->getTool()
139
            ->getTitle();
140
    }
141
142
    /**
143
     * Resource type name (derived from the shortcut node).
144
     */
145
    public function getType(): string
146
    {
147
        return $this->getShortCutNode()
148
            ->getResourceType()
149
            ->getTitle();
150
    }
151
152
    public function getCustomImageUrl(): ?string
153
    {
154
        return $this->customImageUrl;
155
    }
156
157
    public function setCustomImageUrl(?string $customImageUrl): self
158
    {
159
        $this->customImageUrl = $customImageUrl;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Set a custom URL that will be returned by getUrl().
166
     * Use it, for example, to point a CBlog shortcut to:
167
     *   /resources/blog/{courseNodeId}/{blogId}/posts?cid=...&sid=...&gid=0
168
     */
169
    public function setUrlOverride(?string $url): self
170
    {
171
        $this->urlOverride = $url;
172
173
        return $this;
174
    }
175
176
    public function getUrlOverride(): ?string
177
    {
178
        return $this->urlOverride;
179
    }
180
181
    /**
182
     * Set an icon name (e.g., 'mdi-notebook-outline').
183
     */
184
    public function setIcon(?string $icon): self
185
    {
186
        $this->icon = $icon;
187
188
        return $this;
189
    }
190
191
    public function getIcon(): ?string
192
    {
193
        return $this->icon;
194
    }
195
}
196