Passed
Push — master ( e56e7a...209503 )
by Yannick
07:01
created

AbstractTool::isCourseTool()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Tool;
8
9
use Symfony\Component\Serializer\Annotation\Groups;
10
11
abstract class AbstractTool
12
{
13
    #[Groups(['ctool:read'])]
14
    protected string $title;
15
16
    #[Groups(['ctool:read'])]
17
    protected string $titleToShow;
18
19
    #[Groups(['ctool:read'])]
20
    protected string $icon = '';
21
22
    #[Groups(['ctool:read'])]
23
    protected string $category = '';
24
25
    protected string $image;
26
27
    protected array $resourceTypes = [];
28
29
    /**
30
     * Tool scope.
31
     *
32
     * Values can be the following.
33
     *
34
     * - 00 disabled tool
35
     * - 01 course tool
36
     * - 10 global tool
37
     * - 11 global or course or both
38
     */
39
    protected string $scope;
40
41
    public function isCourseTool(): bool
42
    {
43
        return false;
44
    }
45
46
    abstract public function getTitle(): string;
47
48
    public function getTarget(): string
49
    {
50
        return '_self';
51
    }
52
53
    public function getImage(): string
54
    {
55
        return $this->image;
56
    }
57
58
    public function getEntityByResourceType(string $type): ?string
59
    {
60
        return $this->getResourceTypes()[$type] ?? null;
61
    }
62
63
    public function getTypeNameByEntity(string $entityClass): ?string
64
    {
65
        if (empty($this->getResourceTypes())) {
66
            return null;
67
        }
68
69
        $list = array_flip($this->getResourceTypes());
70
71
        if (isset($list[$entityClass])) {
72
            return $list[$entityClass];
73
        }
74
75
        return null;
76
    }
77
78
    public function getResourceTypes(): ?array
79
    {
80
        return $this->resourceTypes;
81
    }
82
83
    public function getTitleToShow(): string
84
    {
85
        $title = $this->getTitle();
86
        // Exception for singular terms that need a plural (with an 's')
87
        switch ($title) {
88
            case 'course_setting':
89
                return 'Course settings';
90
            case 'member':
91
                return 'Users';
92
            case 'announcement':
93
                return 'Announcements';
94
            case 'attendance':
95
                return 'Attendances';
96
            case 'link':
97
                return 'Links';
98
            case 'survey':
99
                return 'Surveys';
100
            default:
101
                return ucfirst(str_replace('_', ' ', $title));
102
        }
103
    }
104
}
105