Passed
Pull Request — master (#5062)
by Angel Fernando Quiroz
06:55
created

CToolProvider::provide()   F

Complexity

Conditions 16
Paths 264

Size

Total Lines 55
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 27
c 1
b 0
f 0
nc 264
nop 3
dl 0
loc 55
rs 3.9333

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\State;
8
9
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
10
use ApiPlatform\Metadata\Operation;
11
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
12
use ApiPlatform\State\ProviderInterface;
13
use Chamilo\CoreBundle\Entity\ResourceLink;
14
use Chamilo\CoreBundle\Entity\User;
15
use Chamilo\CoreBundle\Settings\SettingsManager;
16
use Chamilo\CoreBundle\Tool\ToolChain;
17
use Chamilo\CoreBundle\Traits\CourseFromRequestTrait;
18
use Chamilo\CourseBundle\Entity\CTool;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
use Symfony\Component\Security\Core\Security;
21
22
/**
23
 * @template-implements ProviderInterface<CTool>
24
 */
25
class CToolProvider implements ProviderInterface
26
{
27
    use CourseFromRequestTrait;
28
29
    public function __construct(
30
        private readonly CollectionProvider $provider,
31
        private readonly SettingsManager $settingsManager,
32
        private readonly Security $security,
33
        private readonly ToolChain $toolChain,
34
        protected RequestStack $requestStack,
35
    ) {}
36
37
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
38
    {
39
        /** @var PartialPaginatorInterface $result */
40
        $result = $this->provider->provide($operation, $uriVariables, $context);
41
42
        $request = $this->requestStack->getMainRequest();
43
44
        $studentView = $request ? $request->getSession()->get('studentview') : 'studentview';
45
46
        /** @var User|null $user */
47
        $user = $this->security->getUser();
48
49
        $isAllowToEdit = $user && ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_CURRENT_COURSE_TEACHER'));
50
        $isAllowToSessionEdit = $user && ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_CURRENT_COURSE_TEACHER') || $user->hasRole('ROLE_CURRENT_COURSE_SESSION_TEACHER'));
51
52
        $allowVisibilityInSession = $this->settingsManager->getSetting('course.allow_edit_tool_visibility_in_session');
53
        $session = $this->getSession();
54
55
        $results = [];
56
57
        /** @var CTool $cTool */
58
        foreach ($result as $cTool) {
59
            $toolModel = $this->toolChain->getToolFromName(
60
                $cTool->getTool()->getName()
61
            );
62
63
            if (!$isAllowToEdit && 'admin' === $toolModel->getCategory()) {
64
                continue;
65
            }
66
67
            $resourceLinks = $cTool->getResourceNode()->getResourceLinks();
68
69
            if ($session && $allowVisibilityInSession) {
70
                $sessionLink = $resourceLinks->findFirst(
71
                    fn (int $key, ResourceLink $resourceLink): bool => $resourceLink->getSession()?->getId() === $session->getId()
72
                );
73
74
                if ($sessionLink) {
75
                    $resourceLinks->clear();
76
                    $resourceLinks->add($sessionLink);
77
                }
78
            }
79
80
            if (!$isAllowToSessionEdit || 'studentview' === $studentView) {
81
                $notPublishedLink = ResourceLink::VISIBILITY_PUBLISHED !== $resourceLinks->first()->getVisibility();
82
83
                if ($notPublishedLink) {
84
                    continue;
85
                }
86
            }
87
88
            $results[] = $cTool;
89
        }
90
91
        return $results;
92
    }
93
}
94