UpdateReportQuery::fixReportDefs()   D
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 25
Code Lines 16

Duplication

Lines 13
Ratio 52 %

Importance

Changes 0
Metric Value
dl 13
loc 25
rs 4.909
c 0
b 0
f 0
cc 9
eloc 16
nc 8
nop 7
1
<?php
2
3
namespace Oro\Bundle\SalesBundle\Migrations\Schema\v1_22;
4
5
use Psr\Log\LoggerInterface;
6
7
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper;
8
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
9
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
10
11
class UpdateReportQuery extends ParametrizedMigrationQuery
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function getDescription()
17
    {
18
        $logger = new ArrayLogger();
19
        $this->doExecute($logger, true);
20
21
        return $logger->getMessages();
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function execute(LoggerInterface $logger)
28
    {
29
        $this->doExecute($logger);
30
    }
31
32
    /**
33
     * @param LoggerInterface $logger
34
     * @param bool            $dryRun
35
     */
36
    public function doExecute(LoggerInterface $logger, $dryRun = false)
37
    {
38
        $this->migrateReport($logger, $dryRun);
39
        $this->migrateSegment($logger, $dryRun);
40
    }
41
42
    /**
43
     * @param LoggerInterface $logger
44
     * @param $dryRun
45
     * @param $def
46
     * @param $row
47
     * @throws \Doctrine\DBAL\DBALException
48
     */
49
    protected function updateReport(LoggerInterface $logger, $dryRun, $def, $row)
50
    {
51
        $query = 'UPDATE oro_report SET definition = :definition WHERE id = :id';
52
        $this->executeQuery($logger, $dryRun, $def, $row, $query);
53
    }
54
55
    /**
56
     * @param LoggerInterface $logger
57
     * @param $dryRun
58
     * @param $def
59
     * @param $row
60
     * @throws \Doctrine\DBAL\DBALException
61
     */
62
    protected function updateSegment(LoggerInterface $logger, $dryRun, $def, $row)
63
    {
64
        $query = 'UPDATE oro_segment SET definition = :definition WHERE id = :id';
65
        $this->executeQuery($logger, $dryRun, $def, $row, $query);
66
    }
67
68
    /**
69
     * @param LoggerInterface $logger
70
     * @param $dryRun
71
     */
72 View Code Duplication
    protected function migrateReport(LoggerInterface $logger, $dryRun)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $sql = 'SELECT r.id, r.definition, r.entity FROM oro_report r';
75
76
        $className = 'Oro\Bundle\SalesBundle\Entity\Opportunity';
77
        $oldField = 'status_label';
78
        $newField = 'status';
79
        $this->logQuery($logger, $sql);
80
81
        $rows = $this->connection->fetchAll($sql);
82
        foreach ($rows as $row) {
83
            $def = json_decode($row['definition'], true);
84
            $this->fixReportDefs($logger, $dryRun, $def, $row, $className, $oldField, $newField);
85
        }
86
    }
87
88
    /**
89
     * @param LoggerInterface $logger
90
     * @param $dryRun
91
     */
92 View Code Duplication
    protected function migrateSegment(LoggerInterface $logger, $dryRun)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $sql = 'SELECT s.id, s.definition, s.entity FROM oro_segment s';
95
96
        $className = 'Oro\Bundle\SalesBundle\Entity\Opportunity';
97
        $oldField = 'status_label';
98
        $newField = 'status';
99
        $this->logQuery($logger, $sql);
100
101
        $rows = $this->connection->fetchAll($sql);
102
        foreach ($rows as $row) {
103
            $def = json_decode($row['definition'], true);
104
            $this->fixSegmentDefs($logger, $dryRun, $def, $row, $className, $oldField, $newField);
105
        }
106
    }
107
108
    /**
109
     * @param LoggerInterface $logger
110
     * @param $dryRun
111
     * @param $def
112
     * @param $row
113
     * @param $className
114
     * @param $oldField
115
     * @param $newField
116
     */
117 View Code Duplication
    protected function fixSegmentDefs(LoggerInterface $logger, $dryRun, $def, $row, $className, $oldField, $newField)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        if (isset($def['columns'])) {
120
            foreach ($def['columns'] as $key => $field) {
121
                if (isset($field['name']) && $row['entity'] === $className && $field['name'] === $oldField) {
122
                    $def['columns'][$key]['name'] = $newField;
123
                    $this->updateSegment($logger, $dryRun, $def, $row);
124
                }
125
            }
126
        }
127
        if (isset($def['filters'])) {
128
            foreach ($def['filters'] as $key => $field) {
129
                if (isset($field['columnName'])) {
130
                    $def = $this->processFilterDefinition($def, $row, $className, $oldField, $newField, $field, $key);
131
                    $def = $this->fixFilterCriterion($def, $field, $key);
132
                    $this->updateSegment($logger, $dryRun, $def, $row);
133
                }
134
            }
135
        }
136
    }
137
138
    /**
139
     * @param LoggerInterface $logger
140
     * @param $dryRun
141
     * @param $def
142
     * @param $row
143
     * @param $className
144
     * @param $oldField
145
     * @param $newField
146
     */
147
    protected function fixReportDefs(LoggerInterface $logger, $dryRun, $def, $row, $className, $oldField, $newField)
148
    {
149 View Code Duplication
        if (isset($def['columns'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
            foreach ($def['columns'] as $key => $field) {
151
                if (isset($field['name'])) {
152
                    if ($row['entity'] === $className && $field['name'] === $oldField) {
153
                        $def['columns'][$key]['name'] = $newField;
154
                    } else {
155
                        $def['columns'][$key]['name']
156
                            = str_replace('Opportunity::status_label', 'Opportunity::status', $field['name']);
157
                    }
158
                    $this->updateReport($logger, $dryRun, $def, $row);
159
                }
160
            }
161
        }
162
        if (isset($def['filters'])) {
163
            foreach ($def['filters'] as $key => $field) {
164
                if (isset($field['columnName'])) {
165
                    $def = $this->processFilterDefinition($def, $row, $className, $oldField, $newField, $field, $key);
166
                    $def = $this->fixFilterCriterion($def, $field, $key);
167
                    $this->updateReport($logger, $dryRun, $def, $row);
168
                }
169
            }
170
        }
171
    }
172
173
    /**
174
     * @param LoggerInterface $logger
175
     * @param $dryRun
176
     * @param $def
177
     * @param $row
178
     * @param $query
179
     * @throws \Doctrine\DBAL\DBALException
180
     */
181 View Code Duplication
    protected function executeQuery(LoggerInterface $logger, $dryRun, $def, $row, $query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $params = ['definition' => json_encode($def), 'id' => $row['id']];
184
        $types = ['definition' => 'text', 'id' => 'integer'];
185
        $this->logQuery($logger, $query, $params, $types);
186
        if (!$dryRun) {
187
            $this->connection->executeUpdate($query, $params, $types);
188
        }
189
    }
190
191
    /**
192
     * @param $def
193
     * @param $row
194
     * @param $className
195
     * @param $oldField
196
     * @param $newField
197
     * @param $field
198
     * @param $key
199
     * @return mixed
200
     */
201 View Code Duplication
    protected function processFilterDefinition($def, $row, $className, $oldField, $newField, $field, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
    {
203
        if ($row['entity'] === $className && $field['columnName'] === $oldField) {
204
            $def['filters'][$key]['columnName'] = $newField;
205
        } else {
206
            $def['filters'][$key]['columnName']
207
                = str_replace('Opportunity::status_label', 'Opportunity::status', $field['columnName']);
208
        }
209
210
        return $def;
211
    }
212
213
    /**
214
     * @param $def
215
     * @param $field
216
     * @param $key
217
     * @return array
218
     */
219 View Code Duplication
    protected function fixFilterCriterion($def, $field, $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221
        $paramOldClassName = 'Oro\Bundle\SalesBundle\Entity\OpportunityStatus';
222
        $paramNewClassName = ExtendHelper::buildEnumValueClassName('opportunity_status');
223
        if (isset($field['criterion']['data']['params']['class'])
224
            && $field['criterion']['data']['params']['class'] === $paramOldClassName
225
            && $field['criterion']['filter'] === 'dictionary'
226
        ) {
227
            $def['filters'][$key]['criterion']['data']['params']['class'] = $paramNewClassName;
228
            $def['filters'][$key]['criterion']['filter'] = 'enum';
229
        }
230
231
        return $def;
232
    }
233
}
234