Completed
Pull Request — master (#358)
by Simon
04:39
created

DisableConnectInFrontend   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 62
rs 10
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 6 1
A disableBuyButtonForConnect() 0 12 2
A isConnectArticle() 0 6 1
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\Subscribers;
9
10
use Enlight\Event\SubscriberInterface;
11
use Enlight_Components_Db_Adapter_Pdo_Mysql;
12
13
/**
14
 * The DisableConnectInFrontend subscriber is used, if the user's api key is not valid. In this case, connect products
15
 * cannot be ordered in the frontend.
16
 */
17
class DisableConnectInFrontend implements SubscriberInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $pluginPath;
23
    /**
24
     * @var Enlight_Components_Db_Adapter_Pdo_Mysql
25
     */
26
    private $db;
27
28
    /**
29
     * @param string $pluginPath
30
     * @param Enlight_Components_Db_Adapter_Pdo_Mysql $db
31
     */
32
    public function __construct($pluginPath, Enlight_Components_Db_Adapter_Pdo_Mysql $db)
33
    {
34
        $this->pluginPath = $pluginPath;
35
        $this->db = $db;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            'Enlight_Controller_Action_PostDispatchSecure_Frontend_Detail' => 'disableBuyButtonForConnect'
45
        ];
46
    }
47
48
    /**
49
     * @event Enlight_Controller_Action_PostDispatch_Frontend_Detail
50
     * @param \Enlight_Event_EventArgs $args
51
     */
52
    public function disableBuyButtonForConnect(\Enlight_Event_EventArgs $args)
53
    {
54
        /** @var \Shopware_Controllers_Frontend_Detail $controller */
55
        $controller = $args->getSubject();
56
        $view = $controller->View();
57
58
        $article = $view->getAssign('sArticle');
59
        if ($this->isConnectArticle($article['articleID'])) {
60
            $view->addTemplateDir($this->pluginPath . '/Views/responsive', 'connect');
61
            $view->assign('hideConnect', true);
62
        }
63
    }
64
65
    /**
66
     * Not using the default helper-methods here, in order to keep this small and without any dependencies
67
     * to the SDK
68
     *
69
     * @param $id
70
     * @return string
71
     */
72
    private function isConnectArticle($id)
73
    {
74
        $sql = 'SELECT shop_id FROM s_plugin_connect_items WHERE article_id = ? AND shop_id IS NOT NULL';
75
76
        return $this->db->fetchOne($sql, [$id]);
77
    }
78
}
79