Completed
Push — master ( 535b09...0667cb )
by
unknown
56:23
created

UpdateReport::updateReport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Migrations\Schema\v1_24;
4
5
use Doctrine\DBAL\Schema\Schema;
6
7
use Psr\Log\LoggerInterface;
8
9
use Oro\Bundle\MigrationBundle\Migration\Migration;
10
use Oro\Bundle\MigrationBundle\Migration\OrderedMigrationInterface;
11
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
12
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper;
13
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
14
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
15
16
use OroCRM\Bundle\SalesBundle\Entity\Lead;
17
18
class UpdateReport extends ParametrizedMigrationQuery implements Migration, OrderedMigrationInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getOrder()
24
    {
25
        return 4;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function up(Schema $schema, QueryBag $queries)
32
    {
33
        $queries->addQuery(new self());
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getDescription()
40
    {
41
        $logger = new ArrayLogger();
42
        $this->doExecute($logger, true);
43
44
        return $logger->getMessages();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function execute(LoggerInterface $logger)
51
    {
52
        $this->doExecute($logger);
53
    }
54
55
    /**
56
     * @param LoggerInterface $logger
57
     * @param bool            $dryRun
58
     */
59
    public function doExecute(LoggerInterface $logger, $dryRun = false)
60
    {
61
        $this->migrateReport($logger, $dryRun);
62
        $this->migrateSegment($logger, $dryRun);
63
    }
64
65
    /**
66
     * @param LoggerInterface $logger
67
     * @param bool $dryRun
68
     * @param array $def
69
     * @param array $row
70
     * @throws \Doctrine\DBAL\DBALException
71
     */
72
    protected function updateReport(LoggerInterface $logger, $dryRun, $def, $row)
73
    {
74
        $query = 'UPDATE oro_report SET definition = :definition WHERE id = :id';
75
        $this->executeQuery($logger, $dryRun, $def, $row, $query);
76
    }
77
78
    /**
79
     * @param LoggerInterface $logger
80
     * @param bool $dryRun
81
     * @param array $def
82
     * @param array $row
83
     * @throws \Doctrine\DBAL\DBALException
84
     */
85
    protected function updateSegment(LoggerInterface $logger, $dryRun, $def, $row)
86
    {
87
        $query = 'UPDATE oro_segment SET definition = :definition WHERE id = :id';
88
        $this->executeQuery($logger, $dryRun, $def, $row, $query);
89
    }
90
91
    /**
92
     * @param LoggerInterface $logger
93
     * @param bool $dryRun
94
     */
95 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...
96
    {
97
        $sql = 'SELECT r.id, r.definition, r.entity FROM oro_report r';
98
99
        $className = 'OroCRM\Bundle\SalesBundle\Entity\Lead';
100
        $oldField = 'status_label';
101
        $newField = 'status';
102
        $this->logQuery($logger, $sql);
103
104
        $rows = $this->connection->fetchAll($sql);
105
        foreach ($rows as $row) {
106
            $def = json_decode($row['definition'], true);
107
            $this->fixReportDefs($logger, $dryRun, $def, $row, $className, $oldField, $newField);
108
        }
109
    }
110
111
    /**
112
     * @param LoggerInterface $logger
113
     * @param bool $dryRun
114
     */
115 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...
116
    {
117
        $sql = 'SELECT s.id, s.definition, s.entity FROM oro_segment s';
118
119
        $className = 'OroCRM\Bundle\SalesBundle\Entity\Lead';
120
        $oldField = 'status_label';
121
        $newField = 'status';
122
        $this->logQuery($logger, $sql);
123
124
        $rows = $this->connection->fetchAll($sql);
125
        foreach ($rows as $row) {
126
            $def = json_decode($row['definition'], true);
127
            $this->fixSegmentDefs($logger, $dryRun, $def, $row, $className, $oldField, $newField);
128
        }
129
    }
130
131
    /**
132
     * @param LoggerInterface $logger
133
     * @param bool $dryRun
134
     * @param array $def
135
     * @param array $row
136
     * @param string $className
137
     * @param string $oldField
138
     * @param string $newField
139
     */
140 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...
141
    {
142
        if (isset($def['columns'])) {
143
            foreach ($def['columns'] as $key => $field) {
144
                if (isset($field['name']) && $row['entity'] === $className && $field['name'] === $oldField) {
145
                    $def['columns'][$key]['name'] = $newField;
146
                    $this->updateSegment($logger, $dryRun, $def, $row);
147
                }
148
            }
149
        }
150
        if (isset($def['filters'])) {
151
            foreach ($def['filters'] as $key => $field) {
152
                if (isset($field['columnName'])) {
153
                    $def = $this->processFilterDefinition($def, $row, $className, $oldField, $newField, $field, $key);
154
                    $def = $this->fixFilterCriterion($def, $field, $key);
155
                    $this->updateSegment($logger, $dryRun, $def, $row);
156
                }
157
            }
158
        }
159
    }
160
161
    /**
162
     * @param LoggerInterface $logger
163
     * @param bool $dryRun
164
     * @param array $def
165
     * @param array $row
166
     * @param string $className
167
     * @param string $oldField
168
     * @param string $newField
169
     */
170
    protected function fixReportDefs(LoggerInterface $logger, $dryRun, $def, $row, $className, $oldField, $newField)
171
    {
172 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...
173
            foreach ($def['columns'] as $key => $field) {
174
                if (isset($field['name'])) {
175
                    if ($row['entity'] === $className && $field['name'] === $oldField) {
176
                        $def['columns'][$key]['name'] = $newField;
177
                    } else {
178
                        $def['columns'][$key]['name']
179
                            = str_replace('Lead::status_label', 'Lead::status', $field['name']);
180
                    }
181
                    $this->updateReport($logger, $dryRun, $def, $row);
182
                }
183
            }
184
        }
185
        if (isset($def['filters'])) {
186
            foreach ($def['filters'] as $key => $field) {
187
                if (isset($field['columnName'])) {
188
                    $def = $this->processFilterDefinition($def, $row, $className, $oldField, $newField, $field, $key);
189
                    $def = $this->fixFilterCriterion($def, $field, $key);
190
                    $this->updateReport($logger, $dryRun, $def, $row);
191
                }
192
            }
193
        }
194 View Code Duplication
        if (isset($def['grouping_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...
195
            foreach ($def['grouping_columns'] as $key => $field) {
196
                if (isset($field['name'])) {
197
                    if ($field['name'] === $oldField) {
198
                        $def['grouping_columns'][$key]['name'] = $newField;
199
                    } else {
200
                        $def['grouping_columns'][$key]['name']
201
                            = str_replace('Lead::status_label', 'Lead::status', $field['name']);
202
                    }
203
                    $this->updateReport($logger, $dryRun, $def, $row);
204
                }
205
            }
206
        }
207
    }
208
209
    /**
210
     * @param LoggerInterface $logger
211
     * @param bool $dryRun
212
     * @param array $def
213
     * @param array $row
214
     * @param string $query
215
     * @throws \Doctrine\DBAL\DBALException
216
     */
217 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...
218
    {
219
        $params = ['definition' => json_encode($def), 'id' => $row['id']];
220
        $types = ['definition' => 'text', 'id' => 'integer'];
221
        $this->logQuery($logger, $query, $params, $types);
222
        if (!$dryRun) {
223
            $this->connection->executeUpdate($query, $params, $types);
224
        }
225
    }
226
227
    /**
228
     * @param array $def
229
     * @param array $row
230
     * @param string $className
231
     * @param string $oldField
232
     * @param string $newField
233
     * @param array $field
234
     * @param string $key
235
     * @return mixed
236
     */
237 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...
238
    {
239
        if ($row['entity'] === $className && $field['columnName'] === $oldField) {
240
            $def['filters'][$key]['columnName'] = $newField;
241
        } else {
242
            $def['filters'][$key]['columnName']
243
                = str_replace('Lead::status_label', 'Lead::status', $field['columnName']);
244
        }
245
246
        return $def;
247
    }
248
249
    /**
250
     * @param array $def
251
     * @param array $field
252
     * @param string $key
253
     * @return array
254
     */
255 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...
256
    {
257
        $paramOldClassName = 'OroCRM\Bundle\SalesBundle\Entity\LeadStatus';
258
        $paramNewClassName = ExtendHelper::buildEnumValueClassName(Lead::INTERNAL_STATUS_CODE);
259
        if (isset($field['criterion']['data']['params']['class'])
260
            && $field['criterion']['data']['params']['class'] === $paramOldClassName
261
            && $field['criterion']['filter'] === 'dictionary'
262
        ) {
263
            $def['filters'][$key]['criterion']['data']['params']['class'] = $paramNewClassName;
264
            $def['filters'][$key]['criterion']['filter'] = 'enum';
265
        }
266
267
        return $def;
268
    }
269
}
270