Passed
Push — master ( 212654...8aa330 )
by Angel Fernando Quiroz
11:04
created

PluginRepository::installPlugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Plugin;
10
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\Persistence\ManagerRegistry;
12
13
class PluginRepository extends ServiceEntityRepository
14
{
15
    public function __construct(ManagerRegistry $registry)
16
    {
17
        parent::__construct($registry, Plugin::class);
18
    }
19
20
    /**
21
     * Get all installed plugins.
22
     *
23
     * @return Plugin[]
24
     */
25
    public function getInstalledPlugins(): array
26
    {
27
        return $this->findBy(['installed' => true]);
28
    }
29
30
    /**
31
     * Get all active plugins.
32
     *
33
     * @return Plugin[]
34
     */
35
    public function getActivePlugins(): array
36
    {
37
        return $this->findBy(['installed' => true, 'active' => true]);
38
    }
39
40
    /**
41
     * Check if a plugin is installed.
42
     *
43
     * @param string $pluginName
44
     * @return bool
45
     */
46
    public function isInstalled(string $pluginName): bool
47
    {
48
        return $this->findOneBy(['title' => $pluginName, 'installed' => true]) !== null;
49
    }
50
51
    /**
52
     * Check if a plugin is active.
53
     *
54
     * @param string $pluginName
55
     * @return bool
56
     */
57
    public function isActive(string $pluginName): bool
58
    {
59
        $plugin = $this->findOneBy(['title' => $pluginName, 'installed' => true]);
60
        return $plugin ? $plugin->isActive() : false;
61
    }
62
63
    /**
64
     * Install a plugin.
65
     *
66
     * @param string $pluginName
67
     */
68
    public function installPlugin(string $pluginName): void
69
    {
70
        $plugin = $this->findOneBy(['title' => $pluginName]);
71
72
        if (!$plugin) {
73
            $plugin = new Plugin();
74
            $plugin->setTitle($pluginName)
75
                ->setInstalled(true)
76
                ->setActive(false)
0 ignored issues
show
Bug introduced by
The method setActive() does not exist on Chamilo\CoreBundle\Entity\Plugin. ( Ignorable by Annotation )

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

76
                ->/** @scrutinizer ignore-call */ setActive(false)

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...
77
                ->setVersion('1.0')
78
                ->setAccessUrlId(api_get_current_access_url_id())
79
                ->setConfiguration([]);
80
        } else {
81
            $plugin->setInstalled(true);
82
        }
83
84
        $this->getEntityManager()->persist($plugin);
85
        $this->getEntityManager()->flush();
86
    }
87
88
    /**
89
     * Uninstall a plugin.
90
     *
91
     * @param string $pluginName
92
     */
93
    public function uninstallPlugin(string $pluginName): void
94
    {
95
        $plugin = $this->findOneBy(['title' => $pluginName]);
96
97
        if ($plugin) {
98
            $plugin->setInstalled(false);
99
            $this->getEntityManager()->persist($plugin);
100
            $this->getEntityManager()->flush();
101
        }
102
    }
103
104
    /**
105
     * Enable a plugin.
106
     *
107
     * @param string $pluginName
108
     */
109
    public function enablePlugin(string $pluginName): void
110
    {
111
        $plugin = $this->findOneBy(['title' => $pluginName]);
112
113
        if ($plugin && $plugin->isInstalled()) {
114
            $plugin->setActive(true);
115
            $this->getEntityManager()->persist($plugin);
116
            $this->getEntityManager()->flush();
117
        }
118
    }
119
120
    /**
121
     * Disable a plugin.
122
     *
123
     * @param string $pluginName
124
     */
125
    public function disablePlugin(string $pluginName): void
126
    {
127
        $plugin = $this->findOneBy(['title' => $pluginName]);
128
129
        if ($plugin && $plugin->isInstalled()) {
130
            $plugin->setActive(false);
131
            $this->getEntityManager()->persist($plugin);
132
            $this->getEntityManager()->flush();
133
        }
134
    }
135
}
136