Supplier::markConnectSuppliers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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