Passed
Push — master ( adf470...3fd861 )
by
unknown
09:12 queued 01:12
created

PluginServiceHelper::isPluginEnabled()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Chamilo\CoreBundle\Repository\AccessUrlRelPluginRepository;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11
12
class PluginServiceHelper
13
{
14
    public function __construct(
15
        private ParameterBagInterface $parameterBag,
16
        private AccessUrlRelPluginRepository $pluginRepo,
17
        private AccessUrlHelper $accessUrlHelper,
18
    ) {}
19
20
    public function loadLegacyPlugin(string $pluginName): ?object
21
    {
22
        $projectDir = $this->parameterBag->get('kernel.project_dir');
23
        $pluginPath = $projectDir . '/public/plugin/' . $pluginName . '/src/' . $pluginName . '.php';
24
        $pluginClass = $pluginName;
25
26
        if (!file_exists($pluginPath)) {
27
            return null;
28
        }
29
30
        if (!class_exists($pluginClass)) {
31
            require_once $pluginPath;
32
        }
33
34
        if (class_exists($pluginClass) && method_exists($pluginClass, 'create')) {
35
            return $pluginClass::create();
36
        }
37
38
        return null;
39
    }
40
41
    public function getPluginSetting(string $pluginName, string $settingKey): mixed
42
    {
43
        $plugin = $this->loadLegacyPlugin($pluginName);
44
45
        if (!$plugin || !method_exists($plugin, 'get')) {
46
            return null;
47
        }
48
49
        return $plugin->get($settingKey);
50
    }
51
52
    public function isPluginEnabled(string $pluginName): bool
53
    {
54
        $accessUrl = $this->accessUrlHelper->getCurrent();
55
        if (null === $accessUrl) {
56
            return false;
57
        }
58
59
        $pluginSetting = $this->pluginRepo->findOneByPluginName($pluginName, $accessUrl->getId());
60
61
        return $pluginSetting && $pluginSetting->isActive();
62
    }
63
64
    public function shouldBlockAccessByPositioning(?int $userId, int $courseId, ?int $sessionId): bool
65
    {
66
        if (!$this->isPluginEnabled('Positioning') || !$userId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $userId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
67
            return false;
68
        }
69
70
        $plugin = $this->loadLegacyPlugin('Positioning');
71
72
        if (!$plugin || $plugin->get('block_course_if_initial_exercise_not_attempted') !== 'true') {
73
            return false;
74
        }
75
76
        $initialData = $plugin->getInitialExercise($courseId, $sessionId);
77
78
        if (empty($initialData['exercise_id'])) {
79
            return false;
80
        }
81
82
        $results = \Event::getExerciseResultsByUser(
0 ignored issues
show
Bug introduced by
The method getExerciseResultsByUser() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        /** @scrutinizer ignore-call */ 
83
        $results = \Event::getExerciseResultsByUser(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
            $userId,
84
            (int) $initialData['exercise_id'],
85
            $courseId,
86
            $sessionId
87
        );
88
89
        return empty($results);
90
    }
91
}
92