Completed
Pull Request — master (#4281)
by Craig
05:33
created

ExtensionsModuleInstaller   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 58
rs 10
c 0
b 0
f 0
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
    private $entities = [
26
        ExtensionEntity::class,
27
        ExtensionDependencyEntity::class,
28
        ExtensionVarEntity::class,
29
    ];
30
31
    public function install(): bool
32
    {
33
        $this->schemaTool->create($this->entities);
34
35
        // populate default data
36
        $this->createDefaultData();
37
        $this->setVar('itemsperpage', 40);
38
        $this->setVar('helpUiMode', 'modal');
39
40
        return true;
41
    }
42
43
    public function upgrade(string $oldVersion): bool
44
    {
45
        switch ($oldVersion) {
46
            // 3.7.13 shipped with Core-1.4.3
47
            // 3.7.15 shipped with Core-2.0.15
48
            // version number reset to 3.0.0 at Core 3.0.0
49
            case '2.9.9':
50
                $this->schemaTool->update([ExtensionEntity::class]);
51
        }
52
53
        return true;
54
    }
55
56
    public function uninstall(): bool
57
    {
58
        // Deletion not allowed
59
        return false;
60
    }
61
62
    /**
63
     * Create the default data for the extensions module.
64
     */
65
    private function createDefaultData(): void
66
    {
67
        $scanner = new Scanner();
68
        $jsonPath = realpath(__DIR__ . '/composer.json');
69
        $jsonContent = $scanner->decode($jsonPath);
70
        $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

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