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

Supplier::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
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\Subscribers;
9
10
use Doctrine\DBAL\Connection;
11
use Enlight\Event\SubscriberInterface;
12
13
class Supplier implements SubscriberInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $pluginPath;
19
    /**
20
     * @var \Shopware_Components_Snippet_Manager
21
     */
22
    private $snippetManager;
23
24
    /**
25
     * @var Connection
26
     */
27
    private $connection;
28
29
    /**
30
     * @param string $pluginPath
31
     * @param \Shopware_Components_Snippet_Manager $snippetManager
32
     * @param Connection $connection
33
     */
34
    public function __construct($pluginPath, \Shopware_Components_Snippet_Manager $snippetManager, Connection $connection)
35
    {
36
        $this->pluginPath = $pluginPath;
37
        $this->snippetManager = $snippetManager;
38
        $this->connection = $connection;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public static function getSubscribedEvents()
45
    {
46
        return [
47
            'Enlight_Controller_Action_PostDispatch_Backend_Supplier' => 'extentBackendSupplier',
48
        ];
49
    }
50
51
    /**
52
     * @param \Enlight_Event_EventArgs $args
53
     */
54 View Code Duplication
    public function extentBackendSupplier(\Enlight_Event_EventArgs $args)
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...
55
    {
56
        /** @var $subject \Enlight_Controller_Action */
57
        $subject = $args->getSubject();
58
        $request = $subject->Request();
59
60
        switch ($request->getActionName()) {
61
            case 'load':
62
                $subject->View()->addTemplateDir($this->pluginPath . 'Views/', 'connect');
63
                $this->snippetManager->addConfigDir($this->pluginPath . 'Views/');
64
                $subject->View()->extendsTemplate(
65
                    'backend/supplier/list.js'
66
                );
67
                break;
68
            case 'getSuppliers':
69
                $subject->View()->data = $this->markConnectSuppliers(
70
                    $subject->View()->data
71
                );
72
                break;
73
            default:
74
                break;
75
        }
76
    }
77
78
    /**
79
     * @param array $suppliers
80
     * @return array
81
     */
82
    protected function markConnectSuppliers($suppliers)
83
    {
84
        $supplierIds = array_map(function ($row) {
85
            return $row['id'];
86
        }, $suppliers);
87
88
        $connectSuppliers = $this->getConnectSuppliers($supplierIds);
89
90
        foreach ($suppliers as $index => $supplier) {
91
            $suppliers[$index]['isConnect'] = in_array($supplier['id'], $connectSuppliers);
92
        }
93
94
        return $suppliers;
95
    }
96
97
    /**
98
     * @param array $supplierIds
99
     * @return array
100
     */
101
    protected function getConnectSuppliers($supplierIds)
102
    {
103
        /** @var \Doctrine\DBAL\Connection $conn */
104
        $builder = $this->connection->createQueryBuilder();
105
        $builder->select('supplierID')
106
            ->from('s_articles_supplier_attributes', 'sa')
107
            ->where('sa.supplierID IN (:supplierIds)')
108
            ->andWhere('sa.connect_is_remote = 1')
109
            ->setParameter('supplierIds', $supplierIds, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
110
111
        return array_map(function ($item) {
112
            return $item['supplierID'];
113
        }, $builder->execute()->fetchAll(\PDO::FETCH_ASSOC));
114
    }
115
}
116