Completed
Pull Request — master (#420)
by Tobias
03:25
created

Uninstall   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 165
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A run() 0 9 1
A getCrudService() 0 4 1
A removeMyAttributes() 0 49 2
A deactivateConnectProducts() 0 11 1
A removeEngineElement() 0 10 2
A getSnHttpClient() 0 8 1
A sendUninstallNotificationToSocialNetwork() 0 8 2
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
use ShopwarePlugins\Connect\Components\ConfigFactory;
14
use ShopwarePlugins\Connect\Components\SnHttpClient;
15
16
/**
17
 * Uninstaller of the plugin.
18
 * Currently attribute columns will never be removed, as well as the plugin tables. This can be changed once
19
 * shopware supports asking the user, if he wants to remove the plugin permanently or temporarily
20
 *
21
 * Class Uninstall
22
 * @package ShopwarePlugins\Connect\Bootstrap
23
 */
24
class Uninstall
25
{
26
    /**
27
     * @var \Shopware_Plugins_Backend_SwagConnect_Bootstrap
28
     */
29
    protected $bootstrap;
30
31
    /**
32
     * @var Pdo
33
     */
34
    protected $db;
35
36
    /**
37
     * @var ModelManager
38
     */
39
    protected $modelManager;
40
41
    /**
42
     * @var Menu
43
     */
44
    private $menu;
45
46
    /**
47
     * Setup constructor.
48
     * @param \Shopware_Plugins_Backend_SwagConnect_Bootstrap $bootstrap
49
     * @param ModelManager $modelManager
50
     * @param Pdo $db
51
     * @param Menu $menu
52
     */
53
    public function __construct(
54
        \Shopware_Plugins_Backend_SwagConnect_Bootstrap $bootstrap,
55
        ModelManager $modelManager,
56
        Pdo $db,
57
        Menu $menu
58
    ) {
59
        $this->bootstrap = $bootstrap;
60
        $this->modelManager = $modelManager;
61
        $this->db = $db;
62
        $this->menu = $menu;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function run()
69
    {
70
        $this->sendUninstallNotificationToSocialNetwork();
71
        $this->menu->remove();
72
        $this->deactivateConnectProducts();
73
        $this->removeEngineElement();
74
75
        return true;
76
    }
77
78
    /**
79
     * @return CrudService
80
     */
81
    public function getCrudService()
82
    {
83
        return $this->bootstrap->Application()->Container()->get('shopware_attribute.crud_service');
84
    }
85
86
    /**
87
     * Remove the attributes when uninstalling the plugin
88
     */
89
    public function removeMyAttributes()
90
    {
91
        $crudService = $this->getCrudService();
92
93
        try {
94
            $crudService->delete(
95
                's_order_attributes',
96
                'connect_shop_id'
97
            );
98
            $crudService->delete(
99
                's_order_attributes',
100
                'connect_order_id'
101
            );
102
103
            $crudService->delete(
104
                's_categories_attributes',
105
                'connect_import_mapping'
106
            );
107
108
            $crudService->delete(
109
                's_categories_attributes',
110
                'connect_export_mapping'
111
            );
112
113
            $crudService->delete(
114
                's_categories_attributes',
115
                'connect_imported'
116
            );
117
118
            $crudService->delete(
119
                's_premium_dispatch_attributes',
120
                'connect_allowed'
121
            );
122
123
            $crudService->delete(
124
                's_media_attributes',
125
                'connect_hash'
126
            );
127
128
            $this->modelManager->generateAttributeModels([
129
                's_premium_dispatch_attributes',
130
                's_categories_attributes',
131
                's_order_details_attributes',
132
                's_order_basket_attributes',
133
                's_media_attributes'
134
            ]);
135
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
136
        }
137
    }
138
139
    /**
140
     * Disabled all products imported from shopware Connect
141
     */
142
    public function deactivateConnectProducts()
143
    {
144
        $sql = '
145
        UPDATE s_articles
146
        INNER JOIN s_plugin_connect_items
147
          ON s_plugin_connect_items.article_id = s_articles.id
148
          AND shop_id IS NOT NULL
149
        SET s_articles.active = false
150
        ';
151
        $this->db->exec($sql);
152
    }
153
154
    /**
155
     * Remove an engine element so that the connectProductDescription is not displayed in the article anymore
156
     */
157
    public function removeEngineElement()
158
    {
159
        $repo = $this->modelManager->getRepository('Shopware\Models\Article\Element');
160
        $element = $repo->findOneBy(['name' => 'connectProductDescription']);
161
162
        if ($element) {
163
            $this->modelManager->remove($element);
164
            $this->modelManager->flush();
165
        }
166
    }
167
168
    /**
169
     * @return SnHttpClient
170
     */
171
    private function getSnHttpClient()
172
    {
173
        return new SnHttpClient(
174
            $this->bootstrap->Application()->Container()->get('http_client'),
175
            new \Shopware\Connect\Gateway\PDO(Shopware()->Db()->getConnection()),
176
            ConfigFactory::getConfigInstance()
177
        );
178
    }
179
180
    public function sendUninstallNotificationToSocialNetwork()
181
    {
182
        try {
183
            $this->getSnHttpClient()->sendRequestToConnect('account/supplier-plugin-uninstalled');
184
        } catch (\Exception $e) {
185
            Shopware()->PluginLogger()->warning("The plugin was uninstalled but sending the status to Connect failed with this exception: " . $e->getMessage());
186
        }
187
    }
188
}
189