Search::extendBackendPropertySearch()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 28
rs 9.1608
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 Enlight\Event\SubscriberInterface;
11
use Shopware\Components\Model\ModelManager;
12
use Shopware\Models\Property\Group;
13
use Shopware\Models\Property\Option;
14
use Shopware\Models\Property\Value;
15
16
class Search implements SubscriberInterface
17
{
18
    /**
19
     * @var ModelManager
20
     */
21
    private $modelManager;
22
23
    /**
24
     * @param ModelManager $modelManager
25
     */
26
    public function __construct(
27
        ModelManager $modelManager
28
    ) {
29
        $this->modelManager = $modelManager;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public static function getSubscribedEvents()
36
    {
37
        return [
38
            'Enlight_Controller_Action_PostDispatch_Backend_Search' => 'extendBackendPropertySearch',
39
        ];
40
    }
41
42
    /**
43
     * @param \Enlight_Event_EventArgs $args
44
     */
45
    public function extendBackendPropertySearch(\Enlight_Event_EventArgs $args)
46
    {
47
        /** @var $subject \Enlight_Controller_Action */
48
        $subject = $args->getSubject();
49
        $request = $subject->Request();
50
51
        switch ($request->getActionName()) {
52
            case 'search':
53
                $entity = $request->getParam('entity', null);
54
55
                switch ($entity) {
56
                    case Group::class:
57
                    case Option::class:
58
                    case Value::class:
59
                        $subject->View()->data = $this->markRecordsAsConnect(
60
                            $subject->View()->data,
61
                            $entity
62
                        );
63
                        break;
64
                    default:
65
                        break;
66
                }
67
68
                break;
69
            default:
70
                break;
71
        }
72
    }
73
74
    /**
75
     * @param array $data
76
     * @param string $modelName
77
     * @return array
78
     */
79 View Code Duplication
    private function markRecordsAsConnect($data, $modelName)
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...
80
    {
81
        $result = [];
82
83
        foreach ($data as $row) {
84
            if (!array_key_exists('id', $row)) {
85
                continue;
86
            }
87
            $recordId = $row['id'];
88
            $model = $this->modelManager->getRepository($modelName)->find($recordId);
89
90
            $attribute = null;
91
            if ($model) {
92
                $attribute = $model->getAttribute();
93
            }
94
95
            if ($attribute && $attribute->getConnectIsRemote()) {
96
                $row['connect'] = true;
97
            }
98
99
            $result[] = $row;
100
        }
101
102
        return $result;
103
    }
104
}
105