Completed
Pull Request — master (#358)
by Stefan
03:07
created

DisableConnectInFrontend   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 6 1
A disableBuyButtonForConnect() 0 11 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 Enlight_Components_Db_Adapter_Pdo_Mysql
21
     */
22
    private $db;
23
24
    /**
25
     * @param Enlight_Components_Db_Adapter_Pdo_Mysql $db
26
     */
27
    public function __construct(Enlight_Components_Db_Adapter_Pdo_Mysql $db)
28
    {
29
        $this->db = $db;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public static function getSubscribedEvents()
36
    {
37
        return [
38
            'Enlight_Controller_Action_PostDispatchSecure_Frontend_Detail' => 'disableBuyButtonForConnect'
39
        ];
40
    }
41
42
    /**
43
     * @event Enlight_Controller_Action_PostDispatch_Frontend_Detail
44
     * @param \Enlight_Event_EventArgs $args
45
     */
46
    public function disableBuyButtonForConnect(\Enlight_Event_EventArgs $args)
47
    {
48
        /** @var \Shopware_Controllers_Frontend_Detail $controller */
49
        $controller = $args->getSubject();
50
        $view = $controller->View();
51
52
        $article = $view->getAssign('sArticle');
53
        if ($this->isConnectArticle($article['articleID'])) {
54
            $view->assign('hideConnect', true);
55
        }
56
    }
57
58
    /**
59
     * Not using the default helper-methods here, in order to keep this small and without any dependencies
60
     * to the SDK
61
     *
62
     * @param $id
63
     * @return string
64
     */
65
    private function isConnectArticle($id)
66
    {
67
        $sql = 'SELECT shop_id FROM s_plugin_connect_items WHERE article_id = ? AND shop_id IS NOT NULL';
68
69
        return $this->db->fetchOne($sql, [$id]);
70
    }
71
}
72