Passed
Push — dependabot/github_actions/code... ( 154cc6...3e3012 )
by
unknown
26:36 queued 15:59
created

UsergroupDataProvider::provide()   C

Complexity

Conditions 13
Paths 17

Size

Total Lines 70
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 39
c 1
b 0
f 0
nc 17
nop 3
dl 0
loc 70
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\DataProvider;
8
9
use ApiPlatform\Metadata\Operation;
10
use ApiPlatform\State\ProviderInterface;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Entity\Usergroup;
13
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
14
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository;
15
use Exception;
16
use Symfony\Component\Security\Core\Security;
17
18
final class UsergroupDataProvider implements ProviderInterface
19
{
20
    public function __construct(
21
        private readonly Security $security,
22
        private readonly UsergroupRepository $usergroupRepository,
23
        private readonly IllustrationRepository $illustrationRepository
24
    ) {}
25
26
    /**
27
     * @throws Exception
28
     */
29
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
30
    {
31
        $operationName = $operation->getName();
32
        if ('get_usergroup' === $operationName) {
33
            $groupId = $uriVariables['id'] ?? null;
34
35
            if (!$groupId) {
36
                throw new Exception("Group ID is required for 'get_usergroup' operation");
37
            }
38
39
            $group = $this->usergroupRepository->findGroupById($groupId);
40
41
            if (!$group) {
42
                throw new Exception('Group not found');
43
            }
44
45
            $this->setGroupDetails($group);
46
47
            return [$group];
48
        }
49
50
        if ('search_usergroups' === $operationName) {
51
            $searchTerm = $context['filters']['search'] ?? '';
52
            $groups = $this->usergroupRepository->searchGroups($searchTerm);
53
            foreach ($groups as $group) {
54
                $this->setGroupDetails($group);
55
            }
56
57
            return $groups;
58
        }
59
60
        switch ($operationName) {
61
            case 'get_my_usergroups':
62
                $userId = $context['request_attributes']['_api_filters']['userId'] ?? null;
63
                if (!$userId) {
64
                    /** @var User $user */
65
                    $user = $this->security->getUser();
66
                    $userId = $user?->getId();
67
                }
68
                if (!$userId) {
69
                    throw new Exception('User ID is required');
70
                }
71
                $groups = $this->usergroupRepository->getGroupsByUser($userId, 0);
72
73
                break;
74
75
            case 'get_newest_usergroups':
76
                $groups = $this->usergroupRepository->getNewestGroups();
77
78
                break;
79
80
            case 'get_popular_usergroups':
81
                $groups = $this->usergroupRepository->getPopularGroups();
82
83
                break;
84
85
            default:
86
                $groups = [];
87
88
                break;
89
        }
90
91
        if (\in_array($operationName, ['get_my_usergroups', 'get_newest_usergroups', 'get_popular_usergroups'])) {
92
            /** @var Usergroup $group */
93
            foreach ($groups as $group) {
94
                $this->setGroupDetails($group);
95
            }
96
        }
97
98
        return $groups;
99
    }
100
101
    public function supports(Operation $operation, array $uriVariables = [], array $context = []): bool
102
    {
103
        return Usergroup::class === $operation->getClass();
104
    }
105
106
    private function setGroupDetails(Usergroup $group): void
107
    {
108
        $memberCount = $this->usergroupRepository->countMembers($group->getId());
109
        $group->setMemberCount($memberCount);
110
111
        if ($this->illustrationRepository->hasIllustration($group)) {
112
            $picture = $this->illustrationRepository->getIllustrationUrl($group);
113
            $group->setPictureUrl($picture);
114
        }
115
    }
116
}
117