Completed
Pull Request — master (#4281)
by Craig
04:38
created

ExtensionsModuleInstaller   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 61
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createDefaultData() 0 16 1
A uninstall() 0 4 1
A upgrade() 0 11 2
A install() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ExtensionsModule;
15
16
use Zikula\Bundle\CoreBundle\Composer\MetaData;
17
use Zikula\Bundle\CoreBundle\Composer\Scanner;
18
use Zikula\ExtensionsModule\Entity\ExtensionDependencyEntity;
19
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
20
use Zikula\ExtensionsModule\Entity\ExtensionVarEntity;
21
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
22
23
class ExtensionsModuleInstaller extends AbstractExtensionInstaller
24
{
25
    /**
26
     * @var array
27
     */
28
    private $entities = [
29
        ExtensionEntity::class,
30
        ExtensionDependencyEntity::class,
31
        ExtensionVarEntity::class,
32
    ];
33
34
    public function install(): bool
35
    {
36
        $this->schemaTool->create($this->entities);
37
38
        // populate default data
39
        $this->createDefaultData();
40
        $this->setVar('itemsperpage', 40);
41
        $this->setVar('helpUiMode', 'modal');
42
43
        return true;
44
    }
45
46
    public function upgrade(string $oldVersion): bool
47
    {
48
        switch ($oldVersion) {
49
            // 3.7.13 shipped with Core-1.4.3
50
            // 3.7.15 shipped with Core-2.0.15
51
            // version number reset to 3.0.0 at Core 3.0.0
52
            case '2.9.9':
53
                $this->schemaTool->update([ExtensionEntity::class]);
54
        }
55
56
        return true;
57
    }
58
59
    public function uninstall(): bool
60
    {
61
        // Deletion not allowed
62
        return false;
63
    }
64
65
    /**
66
     * Create the default data for the extensions module.
67
     */
68
    private function createDefaultData(): void
69
    {
70
        $scanner = new Scanner();
71
        $jsonPath = realpath(__DIR__ . '/composer.json');
72
        $jsonContent = $scanner->decode($jsonPath);
73
        $metaData = new MetaData($jsonContent);
0 ignored issues
show
Bug introduced by
It seems like $jsonContent can also be of type boolean; however, parameter $json of Zikula\Bundle\CoreBundle...MetaData::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

73
        $metaData = new MetaData(/** @scrutinizer ignore-type */ $jsonContent);
Loading history...
74
        $metaData->setTranslator($this->getTranslator());
75
        $meta = $metaData->getFilteredVersionInfoArray();
76
        $meta['state'] = Constant::STATE_ACTIVE;
77
        unset($meta['dependencies'], $meta['oldnames']);
78
79
        $entity = new ExtensionEntity();
80
        $entity->merge($meta);
81
82
        $this->entityManager->persist($entity);
83
        $this->entityManager->flush();
84
    }
85
}
86