Completed
Push — master ( 388a93...1a6f67 )
by Jonas
07:51 queued 02:34
created

Uninstall::deactivateConnectProducts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
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
    public function __construct(
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
     * Disabled all products imported from shopware Connect
85
     */
86
    public function deactivateConnectProducts()
87
    {
88
        $sql = '
89
        UPDATE s_articles
90
        INNER JOIN s_plugin_connect_items
91
          ON s_plugin_connect_items.article_id = s_articles.id
92
          AND shop_id IS NOT NULL
93
        SET s_articles.active = false
94
        ';
95
        $this->db->exec($sql);
96
    }
97
98
    /**
99
     * Remove an engine element so that the connectProductDescription is not displayed in the article anymore
100
     */
101
    public function removeEngineElement()
102
    {
103
        $repo = $this->modelManager->getRepository('Shopware\Models\Article\Element');
104
        $element = $repo->findOneBy(['name' => 'connectProductDescription']);
105
106
        if ($element) {
107
            $this->modelManager->remove($element);
108
            $this->modelManager->flush();
109
        }
110
    }
111
}
112