Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#715)
by Alexander
03:39
created

MigrateSettings::updateNecessary()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 27
rs 9.7666
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Updates;
14
15
use TYPO3\CMS\Core\Database\ConnectionPool;
16
use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
17
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Install\Update...baseUpdatedPrerequisite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Install\Updates\UpgradeWizardInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
/**
23
 * Class MigrateSettings
24
 * @internal
25
 */
26
class MigrateSettings implements UpgradeWizardInterface
27
{
28
29
    /**
30
     * Return the identifier for this wizard
31
     * This should be the same string as used in the ext_localconf class registration
32
     *
33
     * @return string
34
     */
35
    public function getIdentifier(): string
36
    {
37
        return self::class;
38
    }
39
40
    /**
41
     * Return the speaking name of this wizard
42
     *
43
     * @return string
44
     */
45
    public function getTitle(): string
46
    {
47
        return 'Migrate Kitodo.Presentation plugins to use Extbase settings naming scheme';
48
    }
49
50
    /**
51
     * Return the description for this wizard
52
     *
53
     * @return string
54
     */
55
    public function getDescription(): string
56
    {
57
        return 'This wizard migrates existing front end plugins of the extension Kitodo.Presentation (dlf) to' .
58
            ' make use of the Extbase naming scheme. Therefore it updates the field values' .
59
            ' "pi_flexform" within the tt_content table';
60
    }
61
62
    /**
63
     * Execute the update
64
     *
65
     * Called when a wizard reports that an update is necessary
66
     *
67
     * @return bool
68
     */
69
    public function executeUpdate(): bool
70
    {
71
        // Get all tt_content data of Kitodo.Presentation and update their flexforms settings
72
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tt_content');
73
74
        /** @var QueryBuilder $queryBuilder */
75
        $queryBuilder = $connection->createQueryBuilder();
76
        $statement = $queryBuilder->select('uid')
77
            ->addSelect('pi_flexform')
78
            ->from('tt_content')
79
            ->where(
80
                $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('list')),
81
                $queryBuilder->expr()->like('list_type', $queryBuilder->createNamedParameter('dlf_%'))
82
            )
83
            ->execute();
84
85
        // Update the found record sets
86
        while ($record = $statement->fetch()) {
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

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

86
        while ($record = /** @scrutinizer ignore-deprecated */ $statement->fetch()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
87
            $queryBuilder = $connection->createQueryBuilder();
88
            $updateResult = $queryBuilder->update('tt_content')
89
                ->where(
90
                    $queryBuilder->expr()->eq(
91
                        'uid',
92
                        $queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT)
93
                    )
94
                )
95
                ->set('pi_flexform', $this->migrateFlexformSettings($record['pi_flexform']))
96
                ->execute();
97
98
            // exit if at least one update statement is not successful
99
            if (!((bool) $updateResult)) {
100
                return false;
101
            }
102
        }
103
104
        return true;
105
    }
106
107
    /**
108
     * Is an update necessary?
109
     *
110
     * Looks for fe plugins in tt_content table to be migrated
111
     *
112
     * @return bool
113
     */
114
    public function updateNecessary(): bool
115
    {
116
        $oldSettingsFound = false;
117
118
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tt_content');
119
120
        /** @var QueryBuilder $queryBuilder */
121
        $queryBuilder = $connection->createQueryBuilder();
122
        $statement = $queryBuilder->select('uid')
123
            ->addSelect('pi_flexform')
124
            ->from('tt_content')
125
            ->where(
126
                $queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter('list')),
127
                $queryBuilder->expr()->like('list_type', $queryBuilder->createNamedParameter('dlf_%'))
128
            )
129
            ->execute();
130
131
        // Update the found record sets
132
        while ($record = $statement->fetch()) {
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

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

132
        while ($record = /** @scrutinizer ignore-deprecated */ $statement->fetch()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
133
            $oldSettingsFound = $this->checkForOldSettings($record['pi_flexform']);
134
            if ($oldSettingsFound === true) {
135
                // We found at least one field to be updated --> break here
136
                break;
137
            }
138
        }
139
140
        return $oldSettingsFound;
141
    }
142
143
    /**
144
     * Returns an array of class names of Prerequisite classes
145
     *
146
     * This way a wizard can define dependencies like "database up-to-date" or
147
     * "reference index updated"
148
     *
149
     * @return string[]
150
     */
151
    public function getPrerequisites(): array
152
    {
153
        return [
154
            DatabaseUpdatedPrerequisite::class
155
        ];
156
    }
157
158
159
    /**
160
     * @param string $oldValue
161
     * @return string
162
     */
163
    protected function migrateFlexformSettings(string $oldValue): string
164
    {
165
        $xml = simplexml_load_string($oldValue);
166
167
        // get all field elements
168
        $fields = $xml->xpath("//field");
169
170
        foreach ($fields as $field) {
171
            // change the index attribute if it doesn't start with 'settings.' yet
172
            if (strpos($field['index'], 'settings.') === false) {
173
                $field['index'] = 'settings.' . $field['index'];
174
            }
175
        }
176
177
        return $xml->asXML();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $xml->asXML() could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
178
179
    }
180
181
    /**
182
     * @param string $flexFormXml
183
     * @return bool
184
     */
185
    protected function checkForOldSettings(string $flexFormXml): bool
186
    {
187
        $xml = simplexml_load_string($flexFormXml);
188
189
        // get all field elements with value of attribute index not containing "settings."
190
        $fields = $xml->xpath("//field[not(starts-with(@index, 'settings.'))]");
191
192
        return (bool) $fields;
193
    }
194
195
}
196