Passed
Push — master ( 912bd1...0b1a33 )
by Marcel
06:53
created

Version3007Date20211003180000::copyReport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 56
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 48
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 56
rs 9.1344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace OCA\Analytics\Migration;
6
7
use Closure;
8
use OCP\DB\ISchemaWrapper;
9
use OCP\IDBConnection;
10
use OCP\DB\QueryBuilder\IQueryBuilder;
11
use OCP\Migration\IOutput;
12
use OCP\Migration\SimpleMigrationStep;
13
14
/**
15
 * Auto-generated migration step: Please modify to your needs!
16
 * sudo -u www-data php /var/www/nextcloud/occ migrations:execute analytics 3007Date20211003180000
17
 */
18
class Version3007Date20211003180000 extends SimpleMigrationStep
19
{
20
21
    /** @var IDBConnection */
22
    private $connection;
23
24
    public function __construct(IDBConnection $connection)
25
    {
26
        $this->connection = $connection;
27
    }
28
29
    /**
30
     * @param IOutput $output
31
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
32
     * @param array $options
33
     */
34
    public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
35
    {
36
    }
37
38
    /**
39
     * @param IOutput $output
40
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
41
     * @param array $options
42
     * @return null|ISchemaWrapper
43
     */
44
    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options)
45
    {
46
        /** @var ISchemaWrapper $schema */
47
        $schema = $schemaClosure();
48
49
        if (!$schema->hasTable('analytics_report')) {
50
            $table = $schema->createTable('analytics_report');
51
            $table->addColumn('id', 'integer', [
52
                'autoincrement' => true,
53
                'notnull' => true,
54
            ]);
55
            $table->addColumn('user_id', 'string', [
56
                'notnull' => true,
57
                'length' => 64,
58
            ]);
59
            $table->addColumn('dataset', 'integer', [
60
                'notnull' => true,
61
            ]);
62
            $table->addColumn('name', 'string', [
63
                'notnull' => true,
64
                'length' => 64,
65
            ]);
66
            $table->addColumn('subheader', 'string', [
67
                'notnull' => false,
68
                'length' => 256,
69
            ]);
70
            $table->addColumn('type', 'integer', [
71
                'notnull' => false,
72
            ]);
73
            $table->addColumn('link', 'string', [
74
                'notnull' => false,
75
                'length' => 500,
76
            ]);
77
            $table->addColumn('visualization', 'string', [
78
                'notnull' => false,
79
                'length' => 10,
80
            ]);
81
            $table->addColumn('chart', 'string', [
82
                'notnull' => false,
83
                'length' => 256,
84
            ]);
85
            $table->addColumn('parent', 'integer', [
86
                'notnull' => false,
87
            ]);
88
            $table->addColumn('dimension1', 'string', [
89
                'notnull' => false,
90
                'length' => 64,
91
            ]);
92
            $table->addColumn('dimension2', 'string', [
93
                'notnull' => false,
94
                'length' => 64,
95
            ]);
96
            $table->addColumn('dimension3', 'string', [
97
                'notnull' => false,
98
                'length' => 64,
99
            ]);
100
            $table->addColumn('value', 'string', [
101
                'notnull' => false,
102
                'length' => 64,
103
            ]);
104
            $table->addColumn('chartoptions', 'string', [
105
                'notnull' => false,
106
                'length' => 1000,
107
            ]);
108
            $table->addColumn('dataoptions', 'string', [
109
                'notnull' => false,
110
                'length' => 1000,
111
            ]);
112
            $table->addColumn('filteroptions', 'string', [
113
                'notnull' => false,
114
                'length' => 1000,
115
            ]);
116
            $table->addColumn('refresh', 'integer', [
117
                'notnull' => false,
118
            ]);
119
            $table->setPrimaryKey(['id']);
120
            $table->addIndex(['user_id'], 'analytics_report_user_id_idx');
121
        }
122
123
        $table = $schema->getTable('analytics_share');
124
        if (!$table->hasColumn('report')) {
125
            $table->addColumn('report', 'integer', [
126
                'notnull' => false,
127
            ]);
128
        }
129
130
        $table = $schema->getTable('analytics_threshold');
131
        if (!$table->hasColumn('report')) {
132
            $table->addColumn('report', 'integer', [
133
                'notnull' => false,
134
            ]);
135
        }
136
137
        return $schema;
138
    }
139
140
    /**
141
     * @param IOutput $output
142
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
143
     * @param array $options
144
     */
145
    public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
146
    {
147
        $reportIdMap = $this->copyReport();
148
        $this->fixShares($reportIdMap);
149
        $this->fixThreshold($reportIdMap);
150
        $this->fixParents($reportIdMap);
151
    }
152
153
    /**
154
     * @return int[]
155
     */
156
    protected function copyReport(): array {
157
        $reportIdMap = [];
158
159
        $insert = $this->connection->getQueryBuilder();
160
        $insert->insert('analytics_report')
161
            ->values([
162
                'user_id' => $insert->createParameter('user_id'),
163
                'dataset' => $insert->createParameter('dataset'),
164
                'name' => $insert->createParameter('name'),
165
                'subheader' => $insert->createParameter('subheader'),
166
                'type' => $insert->createParameter('type'),
167
                'link' => $insert->createParameter('link'),
168
                'visualization' => $insert->createParameter('visualization'),
169
                'chart' => $insert->createParameter('chart'),
170
                'parent' => $insert->createParameter('parent'),
171
                'dimension1' => $insert->createParameter('dimension1'),
172
                'dimension2' => $insert->createParameter('dimension2'),
173
                'dimension3' => $insert->createParameter('dimension3'),
174
                'value' => $insert->createParameter('value'),
175
                'chartoptions' => $insert->createParameter('chartoptions'),
176
                'dataoptions' => $insert->createParameter('dataoptions'),
177
                'filteroptions' => $insert->createParameter('filteroptions'),
178
                'refresh' => $insert->createParameter('refresh'),
179
            ]);
180
181
        $query = $this->connection->getQueryBuilder();
182
        $query->select('*')
183
            ->from('analytics_dataset');
184
185
        $result = $query->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

185
        $result = /** @scrutinizer ignore-deprecated */ $query->execute();

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...
186
        while ($row = $result->fetch()) {
187
            $insert
188
                ->setParameter('user_id', $row['user_id'])
189
                ->setParameter('dataset', (int) $row['id'], IQueryBuilder::PARAM_INT)
190
                ->setParameter('name', $row['name'])
191
                ->setParameter('subheader', $row['subheader'])
192
                ->setParameter('type', (int) $row['type'], IQueryBuilder::PARAM_INT)
193
                ->setParameter('link', $row['link'])
194
                ->setParameter('visualization', $row['visualization'])
195
                ->setParameter('chart', $row['chart'])
196
                ->setParameter('parent', (int) $row['parent'], IQueryBuilder::PARAM_INT)
197
                ->setParameter('dimension1', $row['dimension1'])
198
                ->setParameter('dimension2', $row['dimension2'])
199
                ->setParameter('dimension3', $row['dimension3'])
200
                ->setParameter('value', $row['value'])
201
                ->setParameter('chartoptions', $row['chartoptions'])
202
                ->setParameter('dataoptions', $row['dataoptions'])
203
                ->setParameter('filteroptions', $row['filteroptions'])
204
                ->setParameter('refresh', (int) $row['refresh'], IQueryBuilder::PARAM_INT);
205
            $insert->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

205
            /** @scrutinizer ignore-deprecated */ $insert->execute();

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...
206
207
            $reportIdMap[(int)$row['id']] = $insert->getLastInsertId();
208
        }
209
        $result->closeCursor();
210
211
        return $reportIdMap;
212
    }
213
214
    /**
215
     * @param int[] $reportIdMap
216
     */
217
    protected function fixShares(array $reportIdMap): void {
218
219
        $update = $this->connection->getQueryBuilder();
220
        $update->update('analytics_share')
221
            ->set('report', $update->createParameter('report'))
222
            ->where($update->expr()->eq('dataset', $update->createParameter('dataset')));
223
224
        $query = $this->connection->getQueryBuilder();
225
        $query->select(['id'])
226
            ->from('analytics_dataset');
227
        $result = $query->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

227
        $result = /** @scrutinizer ignore-deprecated */ $query->execute();

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...
228
229
        while ($row = $result->fetch()) {
230
            $update
231
                ->setParameter('report', $reportIdMap[(int) $row['id']])
232
                ->setParameter('dataset', (int) $row['id'])
233
            ;
234
            $update->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

234
            /** @scrutinizer ignore-deprecated */ $update->execute();

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...
235
        }
236
    }
237
238
    /**
239
     * @param int[] $reportIdMap
240
     */
241
    protected function fixThreshold(array $reportIdMap): void {
242
243
        $update = $this->connection->getQueryBuilder();
244
        $update->update('analytics_threshold')
245
            ->set('report', $update->createParameter('report'))
246
            ->where($update->expr()->eq('dataset', $update->createParameter('dataset')));
247
248
        $query = $this->connection->getQueryBuilder();
249
        $query->select(['id'])
250
            ->from('analytics_dataset');
251
        $result = $query->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

251
        $result = /** @scrutinizer ignore-deprecated */ $query->execute();

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...
252
253
        while ($row = $result->fetch()) {
254
            $update
255
                ->setParameter('report', $reportIdMap[(int) $row['id']])
256
                ->setParameter('dataset', (int) $row['id'])
257
            ;
258
            $update->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

258
            /** @scrutinizer ignore-deprecated */ $update->execute();

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...
259
        }
260
    }
261
262
    /**
263
     * @param int[] $reportIdMap
264
     */
265
    protected function fixParents(array $reportIdMap): void {
266
267
        $update = $this->connection->getQueryBuilder();
268
        $update->update('analytics_report')
269
            ->set('parent', $update->createParameter('parent_to'))
270
            ->where($update->expr()->eq('id', $update->createParameter('id')));
271
272
        $query = $this->connection->getQueryBuilder();
273
        $query->select(['id', 'parent'])
274
            ->from('analytics_report')
275
            ->where($query->expr()->neq('parent', $query->createNamedParameter('0')));
276
        $result = $query->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

276
        $result = /** @scrutinizer ignore-deprecated */ $query->execute();

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...
277
278
        while ($row = $result->fetch()) {
279
            $update
280
                ->setParameter('parent_to', $reportIdMap[(int) $row['parent']])
281
                ->setParameter('id', (int) $row['id'])
282
            ;
283
            $update->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

283
            /** @scrutinizer ignore-deprecated */ $update->execute();

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...
284
        }
285
    }
286
287
}