Completed
Push — master ( 5d604f...33b193 )
by Craig
07:57
created

ExtensionsModuleInstaller::defaultData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\ExtensionsModule;
13
14
use Zikula\Bundle\CoreBundle\Bundle\MetaData;
15
use Zikula\Bundle\CoreBundle\Bundle\Scanner;
16
use Zikula\Core\AbstractExtensionInstaller;
17
use Zikula\ExtensionsModule\Api\ExtensionApi;
18
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
19
20
/**
21
 * Installation and upgrade routines for the extensions module.
22
 */
23
class ExtensionsModuleInstaller extends AbstractExtensionInstaller
24
{
25
    /**
26
     * Install the Extensions module.
27
     *
28
     * @return boolean true if installation is successful, false otherwise
29
     */
30
    public function install()
31
    {
32
        $entities = [
33
            'Zikula\ExtensionsModule\Entity\ExtensionEntity',
34
            'Zikula\ExtensionsModule\Entity\ExtensionDependencyEntity',
35
            'Zikula\ExtensionsModule\Entity\ExtensionVarEntity',
36
        ];
37
38
        try {
39
            $this->schemaTool->create($entities);
40
        } catch (\Exception $e) {
41
            return false;
42
        }
43
44
        // populate default data
45
        $this->defaultData();
46
        $this->setVar('itemsperpage', 40);
47
48
        // Initialisation successful
49
        return true;
50
    }
51
52
    /**
53
     * Upgrade the module from an old version.
54
     *
55
     * This function must consider all the released versions of the module!
56
     * If the upgrade fails at some point, it returns the last upgraded version.
57
     *
58
     * @param string $oldversion Version number string to upgrade from
0 ignored issues
show
Documentation introduced by
There is no parameter named $oldversion. Did you maybe mean $oldVersion?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
59
     *
60
     * @return  boolean|string True on success, last valid version string or false if fails
61
     */
62
    public function upgrade($oldVersion)
63
    {
64
        // Upgrade dependent on old version number
65
        switch ($oldVersion) {
66
            case '3.7.10':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
67
                // Load DB connection
68
                $connection = $this->entityManager->getConnection();
69
70
                // increase length of some hook table fields from 20 to 60
71
                $commands = [];
72
                $commands[] = "ALTER TABLE `hook_provider` CHANGE `method` `method` VARCHAR(60) NOT NULL";
73
                $commands[] = "ALTER TABLE `hook_runtime` CHANGE `method` `method` VARCHAR(60) NOT NULL";
74
75
                foreach ($commands as $sql) {
76
                    $stmt = $connection->executeQuery($sql);
0 ignored issues
show
Unused Code introduced by
$stmt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
                }
78
            case '3.7.11':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
79
                $this->schemaTool->update(['Zikula\ExtensionsModule\Entity\ExtensionEntity']);
80
            case '3.7.12':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
81
                $this->setVar('itemsperpage', 40);
82
            case '3.7.13':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
83
                $this->schemaTool->update(['Zikula\ExtensionsModule\Entity\ExtensionEntity']);
84
            case '3.7.14':
85
                // future upgrade routines
86
        }
87
88
        // Update successful
89
        return true;
90
    }
91
92
    /**
93
     * delete the modules module
94
     *
95
     * This function is only ever called once during the lifetime of a particular
96
     * module instance.
97
     *
98
     * Since the modules module should never be deleted we'all always return false here
99
     * @return boolean false this module cannot be deleted
100
     */
101
    public function uninstall()
102
    {
103
        // Deletion not allowed
104
        return false;
105
    }
106
107
    /**
108
     * Create the default data for the Extensions module.
109
     *
110
     * @return void
111
     */
112
    public function defaultData()
113
    {
114
        $scanner = new Scanner();
115
        $jsonPath = realpath(__DIR__ . '/composer.json');
116
        $jsonContent = $scanner->decode($jsonPath);
117
        $metaData = new MetaData($jsonContent);
118
        if (!empty($this->container)) {
119
            $metaData->setTranslator($this->container->get('translator'));
120
        }
121
        $meta = $metaData->getFilteredVersionInfoArray();
122
        $meta['state'] = ExtensionApi::STATE_ACTIVE;
123
        unset($meta['dependencies']);
124
        unset($meta['oldnames']);
125
126
        $entity = new ExtensionEntity();
127
        $entity->merge($meta);
128
129
        $this->entityManager->persist($entity);
130
        $this->entityManager->flush();
131
    }
132
}
133