Completed
Pull Request — master (#420)
by Tobias
12:51
created

Uninstall::sendUninstallNotificationToSocialNetwork()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Bootstrap;
9
10
use Shopware\Bundle\AttributeBundle\Service\CrudService;
11
use Shopware\Components\Model\ModelManager;
12
use Enlight_Components_Db_Adapter_Pdo_Mysql as Pdo;
13
14
/**
15
 * Uninstaller of the plugin.
16
 * Currently attribute columns will never be removed, as well as the plugin tables. This can be changed once
17
 * shopware supports asking the user, if he wants to remove the plugin permanently or temporarily
18
 *
19
 * Class Uninstall
20
 * @package ShopwarePlugins\Connect\Bootstrap
21
 */
22
class Uninstall
23
{
24
    /**
25
     * @var \Shopware_Plugins_Backend_SwagConnect_Bootstrap
26
     */
27
    protected $bootstrap;
28
29
    /**
30
     * @var Pdo
31
     */
32
    protected $db;
33
34
    /**
35
     * @var ModelManager
36
     */
37
    protected $modelManager;
38
39
    /**
40
     * @var Menu
41
     */
42
    private $menu;
43
44
    /**
45
     * Setup constructor.
46
     * @param \Shopware_Plugins_Backend_SwagConnect_Bootstrap $bootstrap
47
     * @param ModelManager $modelManager
48
     * @param Pdo $db
49
     * @param Menu $menu
50
     */
51 View Code Duplication
    public function __construct(
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...
52
        \Shopware_Plugins_Backend_SwagConnect_Bootstrap $bootstrap,
53
        ModelManager $modelManager,
54
        Pdo $db,
55
        Menu $menu
56
    ) {
57
        $this->bootstrap = $bootstrap;
58
        $this->modelManager = $modelManager;
59
        $this->db = $db;
60
        $this->menu = $menu;
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function run()
67
    {
68
        $this->menu->remove();
69
        $this->deactivateConnectProducts();
70
        $this->removeEngineElement();
71
72
        return true;
73
    }
74
75
    /**
76
     * @return CrudService
77
     */
78
    public function getCrudService()
79
    {
80
        return $this->bootstrap->Application()->Container()->get('shopware_attribute.crud_service');
81
    }
82
83
    /**
84
     * Remove the attributes when uninstalling the plugin
85
     */
86
    public function removeMyAttributes()
87
    {
88
        $crudService = $this->getCrudService();
89
90
        try {
91
            $crudService->delete(
92
                's_order_attributes',
93
                'connect_shop_id'
94
            );
95
            $crudService->delete(
96
                's_order_attributes',
97
                'connect_order_id'
98
            );
99
100
            $crudService->delete(
101
                's_categories_attributes',
102
                'connect_import_mapping'
103
            );
104
105
            $crudService->delete(
106
                's_categories_attributes',
107
                'connect_export_mapping'
108
            );
109
110
            $crudService->delete(
111
                's_categories_attributes',
112
                'connect_imported'
113
            );
114
115
            $crudService->delete(
116
                's_premium_dispatch_attributes',
117
                'connect_allowed'
118
            );
119
120
            $crudService->delete(
121
                's_media_attributes',
122
                'connect_hash'
123
            );
124
125
            $this->modelManager->generateAttributeModels([
126
                's_premium_dispatch_attributes',
127
                's_categories_attributes',
128
                's_order_details_attributes',
129
                's_order_basket_attributes',
130
                's_media_attributes'
131
            ]);
132
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
133
        }
134
    }
135
136
    /**
137
     * Disabled all products imported from shopware Connect
138
     */
139
    public function deactivateConnectProducts()
140
    {
141
        $sql = '
142
        UPDATE s_articles
143
        INNER JOIN s_plugin_connect_items
144
          ON s_plugin_connect_items.article_id = s_articles.id
145
          AND shop_id IS NOT NULL
146
        SET s_articles.active = false
147
        ';
148
        $this->db->exec($sql);
149
    }
150
151
    /**
152
     * Remove an engine element so that the connectProductDescription is not displayed in the article anymore
153
     */
154
    public function removeEngineElement()
155
    {
156
        $repo = $this->modelManager->getRepository('Shopware\Models\Article\Element');
157
        $element = $repo->findOneBy(['name' => 'connectProductDescription']);
158
159
        if ($element) {
160
            $this->modelManager->remove($element);
161
            $this->modelManager->flush();
162
        }
163
    }
164
}
165