Passed
Push — master ( 4c7952...221fe6 )
by Angel Fernando Quiroz
08:53
created

PluginHelper::getPluginSetting()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Helpers;
8
9
use Chamilo\CoreBundle\Repository\AccessUrlRelPluginRepository;
10
use Event;
11
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
12
13
readonly class PluginHelper
14
{
15
    public function __construct(
16
        private ParameterBagInterface $parameterBag,
17
        private AccessUrlRelPluginRepository $pluginRepo,
18
        private AccessUrlHelper $accessUrlHelper,
19
    ) {}
20
21
    public function loadLegacyPlugin(string $pluginName): ?object
22
    {
23
        $projectDir = $this->parameterBag->get('kernel.project_dir');
24
        $pluginPath = $projectDir.'/public/plugin/'.$pluginName.'/src/'.$pluginName.'.php';
25
        $pluginClass = $pluginName;
26
27
        if (!file_exists($pluginPath)) {
28
            return null;
29
        }
30
31
        if (!class_exists($pluginClass)) {
32
            require_once $pluginPath;
33
        }
34
35
        if (class_exists($pluginClass) && method_exists($pluginClass, 'create')) {
36
            return $pluginClass::create();
37
        }
38
39
        return null;
40
    }
41
42
    public function getPluginSetting(string $pluginName, string $settingKey): mixed
43
    {
44
        $plugin = $this->loadLegacyPlugin($pluginName);
45
46
        if (!$plugin || !method_exists($plugin, 'get')) {
47
            return null;
48
        }
49
50
        return $plugin->get($settingKey);
51
    }
52
53
    public function isPluginEnabled(string $pluginName): bool
54
    {
55
        $accessUrl = $this->accessUrlHelper->getCurrent();
56
        if (null === $accessUrl) {
57
            return false;
58
        }
59
60
        $pluginSetting = $this->pluginRepo->findOneByPluginName($pluginName, $accessUrl->getId());
61
62
        return $pluginSetting && $pluginSetting->isActive();
63
    }
64
65
    public function shouldBlockAccessByPositioning(?int $userId, int $courseId, ?int $sessionId): bool
66
    {
67
        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...
68
            return false;
69
        }
70
71
        $plugin = $this->loadLegacyPlugin('Positioning');
72
73
        if (!$plugin || 'true' !== $plugin->get('block_course_if_initial_exercise_not_attempted')) {
74
            return false;
75
        }
76
77
        $initialData = $plugin->getInitialExercise($courseId, $sessionId);
78
79
        if (empty($initialData['exercise_id'])) {
80
            return false;
81
        }
82
83
        $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

83
        /** @scrutinizer ignore-call */ 
84
        $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...
84
            $userId,
85
            (int) $initialData['exercise_id'],
86
            $courseId,
87
            $sessionId
88
        );
89
90
        return empty($results);
91
    }
92
}
93