Completed
Push — 1.9 ( 5c3e2e...1529fd )
by
unknown
60:20
created

FillActivityAssociationTables::up()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 8.8571
cc 1
eloc 19
nc 1
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Migrations\Schema\v1_37;
4
5
use Doctrine\DBAL\Types\Type;
6
use Doctrine\DBAL\Schema\Schema;
7
8
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
9
use Oro\Bundle\MigrationBundle\Migration\Migration;
10
use Oro\Bundle\MigrationBundle\Migration\SqlMigrationQuery;
11
use Oro\Bundle\MigrationBundle\Migration\ParametrizedSqlMigrationQuery;
12
use Oro\Bundle\MigrationBundle\Migration\OrderedMigrationInterface;
13
14
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;
15
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;
16
17
use Oro\Bundle\ActivityBundle\Migration\Extension\ActivityExtension;
18
use Oro\Bundle\ActivityBundle\Migration\Extension\ActivityExtensionAwareInterface;
19
20
use Oro\Bundle\ActivityListBundle\Migration\Extension\ActivityListExtension;
21
use Oro\Bundle\ActivityListBundle\Migration\Extension\ActivityListExtensionAwareInterface;
22
23
class FillActivityAssociationTables implements
24
    Migration,
25
    OrderedMigrationInterface,
26
    ExtendExtensionAwareInterface,
27
    ActivityExtensionAwareInterface,
28
    ActivityListExtensionAwareInterface
29
{
30
    /** @var ExtendExtension */
31
    protected $extendExtension;
32
33
    /** @var ActivityExtension */
34
    protected $activityExtension;
35
36
    /** @var ActivityListExtension */
37
    protected $activityListExtension;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function setActivityExtension(ActivityExtension $activityExtension)
43
    {
44
        $this->activityExtension = $activityExtension;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function setActivityListExtension(ActivityListExtension $activityListExtension)
51
    {
52
        $this->activityListExtension = $activityListExtension;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function setExtendExtension(ExtendExtension $extendExtension)
59
    {
60
        $this->extendExtension = $extendExtension;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getOrder()
67
    {
68
        return 2;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function up(Schema $schema, QueryBag $queries)
75
    {
76
        $this->fillActivityTables($queries);
77
        $this->fillActivityListTables($queries);
78
79
        // Remove orocrm_magento_cart_emails
80
        $table = $schema->getTable('orocrm_magento_cart_emails');
81
        $table->removeForeignKey('FK_11B0F84B1AD5CDBF');
82
        $table->removeForeignKey('FK_11B0F84BA832C1C9');
83
        $schema->dropTable('orocrm_magento_cart_emails');
84
85
        // Remove orocrm_magento_cart_calls
86
        $table = $schema->getTable('orocrm_magento_cart_calls');
87
        $table->removeForeignKey('FK_83A847751AD5CDBF');
88
        $table->removeForeignKey('FK_83A8477550A89B2C');
89
        $schema->dropTable('orocrm_magento_cart_calls');
90
91
        // Remove orocrm_magento_order_emails
92
        $table = $schema->getTable('orocrm_magento_order_emails');
93
        $table->removeForeignKey('FK_10E2A9508D9F6D38');
94
        $table->removeForeignKey('FK_10E2A950A832C1C9');
95
        $schema->dropTable('orocrm_magento_order_emails');
96
97
        // Remove orocrm_magento_order_calls
98
        $table = $schema->getTable('orocrm_magento_order_calls');
99
        $table->removeForeignKey('FK_A885A3450A89B2C');
100
        $table->removeForeignKey('FK_A885A348D9F6D38');
101
        $schema->dropTable('orocrm_magento_order_calls');
102
    }
103
104
    /**
105
     * @param QueryBag $queries
106
     */
107
    protected function fillActivityTables(QueryBag $queries)
108
    {
109
        $queries->addPreQuery(
110
            new SqlMigrationQuery(
111
                [
112
                    $this->getFillCartEmailActivityQuery(),
113
                    $this->getFillCartCallActivityQuery(),
114
                    $this->getFillOrderEmailActivityQuery(),
115
                    $this->getFillOrderCallActivityQuery()
116
                ]
117
            )
118
        );
119
    }
120
121
    /**
122
     * @param QueryBag $queries
123
     */
124
    protected function fillActivityListTables(QueryBag $queries)
125
    {
126
        // Fill activitylists tables
127
        $queries->addPreQuery(
128
            new ParametrizedSqlMigrationQuery(
129
                $this->getFillCartEmailActivityListQuery(),
130
                ['class' => 'Oro\Bundle\EmailBundle\Entity\Email'],
131
                ['class' => Type::STRING]
132
            )
133
        );
134
        $queries->addPreQuery(
135
            new ParametrizedSqlMigrationQuery(
136
                $this->getFillCartCallActivityListQuery(),
137
                ['class' => 'OroCRM\Bundle\CallBundle\Entity\Call'],
138
                ['class' => Type::STRING]
139
            )
140
        );
141
142
        $queries->addPreQuery(
143
            new ParametrizedSqlMigrationQuery(
144
                $this->getFillOrderEmailActivityListQuery(),
145
                ['class' => 'Oro\Bundle\EmailBundle\Entity\Email'],
146
                ['class' => Type::STRING]
147
            )
148
        );
149
        $queries->addPreQuery(
150
            new ParametrizedSqlMigrationQuery(
151
                $this->getFillOrderCallActivityListQuery(),
152
                ['class' => 'OroCRM\Bundle\CallBundle\Entity\Call'],
153
                ['class' => Type::STRING]
154
            )
155
        );
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    protected function getFillCartEmailActivityQuery()
162
    {
163
        $sql = 'INSERT INTO %s (email_id, cart_id)' .
164
               ' SELECT email_id, cart_id' .
165
               ' FROM orocrm_magento_cart_emails';
166
167
        return sprintf($sql, $this->activityExtension->getAssociationTableName('oro_email', 'orocrm_magento_cart'));
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    protected function getFillCartCallActivityQuery()
174
    {
175
        $sql = 'INSERT INTO %s (call_id, cart_id)' .
176
               ' SELECT call_id, cart_id' .
177
               ' FROM orocrm_magento_cart_calls';
178
179
        return sprintf($sql, $this->activityExtension->getAssociationTableName('orocrm_call', 'orocrm_magento_cart'));
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    protected function getFillOrderEmailActivityQuery()
186
    {
187
        $sql = 'INSERT INTO %s (email_id, order_id)' .
188
               ' SELECT email_id, order_id' .
189
               ' FROM orocrm_magento_order_emails';
190
191
        return sprintf($sql, $this->activityExtension->getAssociationTableName('oro_email', 'orocrm_magento_order'));
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    protected function getFillOrderCallActivityQuery()
198
    {
199
        $sql = 'INSERT INTO %s (call_id, order_id)' .
200
               ' SELECT call_id, order_id' .
201
               ' FROM orocrm_magento_order_calls';
202
203
        return sprintf($sql, $this->activityExtension->getAssociationTableName('orocrm_call', 'orocrm_magento_order'));
204
    }
205
206
    /**
207
     * @return string
208
     */
209 View Code Duplication
    protected function getFillCartEmailActivityListQuery()
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...
210
    {
211
        $sql = 'INSERT INTO %s (activitylist_id, cart_id)' .
212
               ' SELECT al.id, rel.cart_id' .
213
               ' FROM oro_activity_list al' .
214
               ' JOIN %s rel ON rel.email_id = al.related_activity_id' .
215
               ' AND al.related_activity_class = :class';
216
217
        return sprintf(
218
            $sql,
219
            $this->activityListExtension->getAssociationTableName('orocrm_magento_cart'),
220
            $this->activityExtension->getAssociationTableName('oro_email', 'orocrm_magento_cart')
221
        );
222
    }
223
224
    /**
225
     * @return string
226
     */
227 View Code Duplication
    protected function getFillCartCallActivityListQuery()
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...
228
    {
229
        $sql = 'INSERT INTO %s (activitylist_id, cart_id)' .
230
               ' SELECT al.id, rel.cart_id' .
231
               ' FROM oro_activity_list al' .
232
               ' JOIN %s rel ON rel.call_id = al.related_activity_id' .
233
               ' AND al.related_activity_class = :class';
234
235
        return sprintf(
236
            $sql,
237
            $this->activityListExtension->getAssociationTableName('orocrm_magento_cart'),
238
            $this->activityExtension->getAssociationTableName('orocrm_call', 'orocrm_magento_cart')
239
        );
240
    }
241
242
    /**
243
     * @return string
244
     */
245 View Code Duplication
    protected function getFillOrderEmailActivityListQuery()
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...
246
    {
247
        $sql = 'INSERT INTO %s (activitylist_id, order_id)' .
248
               ' SELECT al.id, rel.order_id' .
249
               ' FROM oro_activity_list al' .
250
               ' JOIN %s rel ON rel.email_id = al.related_activity_id' .
251
               ' AND al.related_activity_class = :class';
252
253
        return sprintf(
254
            $sql,
255
            $this->activityListExtension->getAssociationTableName('orocrm_magento_order'),
256
            $this->activityExtension->getAssociationTableName('oro_email', 'orocrm_magento_order')
257
        );
258
    }
259
260
    /**
261
     * @return string
262
     */
263 View Code Duplication
    protected function getFillOrderCallActivityListQuery()
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...
264
    {
265
        $sql = 'INSERT INTO %s (activitylist_id, order_id)' .
266
               ' SELECT al.id, rel.order_id' .
267
               ' FROM oro_activity_list al' .
268
               ' JOIN %s rel ON rel.call_id = al.related_activity_id' .
269
               ' AND al.related_activity_class = :class';
270
271
        return sprintf(
272
            $sql,
273
            $this->activityListExtension->getAssociationTableName('orocrm_magento_order'),
274
            $this->activityExtension->getAssociationTableName('orocrm_call', 'orocrm_magento_order')
275
        );
276
    }
277
}
278