Property::markAssignsAsConnect()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 25
loc 25
rs 9.2088
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
13
class Property implements SubscriberInterface
14
{
15
    /**
16
     * @var ModelManager
17
     */
18
    private $modelManager;
19
20
    /**
21
     * @param ModelManager $modelManager
22
     */
23
    public function __construct(
24
        ModelManager $modelManager
25
    ) {
26
        $this->modelManager = $modelManager;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public static function getSubscribedEvents()
33
    {
34
        return [
35
            'Enlight_Controller_Action_PostDispatch_Backend_Property' => 'extendBackendProperty',
36
        ];
37
    }
38
39
    /**
40
     * @param \Enlight_Event_EventArgs $args
41
     */
42
    public function extendBackendProperty(\Enlight_Event_EventArgs $args)
43
    {
44
        /** @var $subject \Enlight_Controller_Action */
45
        $subject = $args->getSubject();
46
        $request = $subject->Request();
47
48
        switch ($request->getActionName()) {
49
            case 'load':
50
                $subject->View()->extendsTemplate(
51
                    'backend/property/view/main/group_grid_connect.js'
52
                );
53
54
                $subject->View()->extendsTemplate(
55
                    'backend/property/model/group_connect.js'
56
                );
57
58
                $subject->View()->extendsTemplate(
59
                    'backend/property/view/main/set_grid_connect.js'
60
                );
61
62
                $subject->View()->extendsTemplate(
63
                    'backend/property/model/set_connect.js'
64
                );
65
66
                $subject->View()->extendsTemplate(
67
                    'backend/property/view/main/option_grid_connect.js'
68
                );
69
70
                $subject->View()->extendsTemplate(
71
                    'backend/property/model/option_connect.js'
72
                );
73
74
                $subject->View()->extendsTemplate(
75
                    'backend/property/view/main/set_assign_grid_connect.js'
76
                );
77
78
                $subject->View()->extendsTemplate(
79
                    'backend/property/model/set_assign_connect.js'
80
                );
81
82
                break;
83
            case 'getGroups':
84
                // The "Groups" in the frontend are actually "s_filter_options" in the database and the entity is called Option ?!
85
                $subject->View()->data = $this->markRecordsAsConnect(
86
                    $subject->View()->data,
87
                    'Shopware\Models\Property\Option'
88
                );
89
                break;
90
            case 'getSets':
91
                // The "Sets" in the frontend are actually "s_filter" in the database and the entity is called Group ?!
92
                $subject->View()->data = $this->markRecordsAsConnect(
93
                    $subject->View()->data,
94
                    'Shopware\Models\Property\Group'
95
                );
96
                break;
97
            case 'getOptions':
98
                // The "Options" in the frontend are actually "s_filter_values" in the database and the entity is called Value ?!
99
                $subject->View()->data = $this->markRecordsAsConnect(
100
                    $subject->View()->data,
101
                    'Shopware\Models\Property\Value'
102
                );
103
                break;
104
            case 'getSetAssigns':
105
                $subject->View()->data = $this->markAssignsAsConnect(
106
                    $subject->View()->data
107
                );
108
                break;
109
            default:
110
                break;
111
        }
112
    }
113
114
    /**
115
     * @param array $data
116
     * @param string $modelName
117
     * @return array
118
     */
119 View Code Duplication
    public function markRecordsAsConnect(array $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...
120
    {
121
        $result = [];
122
123
        foreach ($data as $row) {
124
            $recordId = $row['id'];
125
            $model = $this->modelManager->getRepository($modelName)->find($recordId);
126
127
            $attribute = null;
128
            if ($model) {
129
                $attribute = $model->getAttribute();
130
            }
131
132
            if ($attribute && $attribute->getConnectIsRemote()) {
133
                $row['connect'] = true;
134
            }
135
136
            $result[] = $row;
137
        }
138
139
        return $result;
140
    }
141
142
    /**
143
     * @param array $data
144
     * @return array
145
     */
146 View Code Duplication
    public function markAssignsAsConnect(array $data)
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...
147
    {
148
        // The set assigns display assigned Groups
149
        // The "Groups" in the frontend are actually "s_filter_options" in the database and the entity is called Option ?!
150
        $setAssigns = [];
151
152
        foreach ($data as $setAssign) {
153
            $optionId = $setAssign['groupId'];
154
            /** @var \Shopware\Models\Property\Option $groupModel */
155
            $optionModel = $this->modelManager->getRepository('Shopware\Models\Property\Option')->find($optionId);
156
157
            $attribute = null;
158
            if ($optionModel) {
159
                $attribute = $optionModel->getAttribute();
160
            }
161
162
            if ($attribute && $attribute->getConnectIsRemote()) {
163
                $setAssign['connect'] = true;
164
            }
165
166
            $setAssigns[] = $setAssign;
167
        }
168
169
        return $setAssigns;
170
    }
171
}
172