Completed
Push — master ( 335c44...a95920 )
by Jonas
05:50 queued 02:51
created

Shopware_Controllers_Backend_LastChanges   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 171
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getChangedProductsAction() 0 23 2
D applyChangesAction() 0 90 13
A getHelper() 0 4 1
A getConnectFactory() 0 8 2
A getModelManager() 0 4 1
A getImageImport() 0 9 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
use ShopwarePlugins\Connect\Components\ImageImport;
9
use Shopware\Models\Article\Price;
10
11
class Shopware_Controllers_Backend_LastChanges extends \Shopware_Controllers_Backend_ExtJs
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /**
14
     * @var \ShopwarePlugins\Connect\Components\ConnectFactory
15
     */
16
    private $factory;
17
18
    /**
19
     * Get a list of products with remote changes which have not been applied
20
     */
21
    public function getChangedProductsAction()
22
    {
23
        /** @var \Shopware\CustomModels\Connect\AttributeRepository $connectAttributeRepository */
24
        $connectAttributeRepository = $this->getModelManager()->getRepository('\Shopware\CustomModels\Connect\Attribute');
25
        $builder = $connectAttributeRepository->getChangedProducts(
26
            $this->Request()->getParam('start', 0),
27
            $this->Request()->getParam('limit', 20),
28
            $this->Request()->getParam('sort', [])
29
        );
30
31
        $total = $builder->execute()->rowCount();
32
        $data = $builder->execute()->fetchAll(\PDO::FETCH_ASSOC);
33
34
        foreach ($data as $key => $record) {
35
            $data[$key]['images'] = implode('|', $this->getImageImport()->getImagesForDetail($record['id']));
36
        }
37
38
        $this->View()->assign([
39
            'success' => true,
40
            'data' => $data,
41
            'total' => $total
42
        ]);
43
    }
44
45
    /**
46
     * Apply given changes to product
47
     *
48
     * @throws \RuntimeException
49
     */
50
    public function applyChangesAction()
51
    {
52
        $type = $this->Request()->getParam('type');
53
        $value = $this->Request()->getParam('value');
54
        $detailId = $this->Request()->getParam('detailId');
55
56
        /** @var \Shopware\Models\Article\Detail $detail */
57
        $detail = $this->getModelManager()->getRepository('\Shopware\Models\Article\Detail')->find($detailId);
58
59
        if (!$detail) {
60
            $this->View()->assign('success', false);
61
            $message = Shopware()->Snippets()->getNamespace('backend/connect/view/main')->get(
62
                'changed_products/error/wrongArticleDetailId',
63
                'The product was not found',
64
                true
65
            );
66
            $this->View()->assign('message', $message);
67
68
            return;
69
        }
70
71
        /** @var \Shopware\Models\Article\Article $article */
72
        $article = $detail->getArticle();
73
        $connectAttribute = $this->getHelper()->getOrCreateConnectAttributeByModel($detail);
74
75
        $updateFlags = $this->getHelper()->getUpdateFlags();
76
        $updateFlagsByName = array_flip($updateFlags);
77
        $flag = $updateFlagsByName[$type];
78
79
        switch ($type) {
80
            case 'shortDescription':
81
                $article->setDescription($value);
82
                break;
83
            case 'longDescription':
84
                $article->setDescriptionLong($value);
85
                break;
86
            case 'additionalDescription':
87
                $detail->getAttribute()->setConnectProductDescription($value);
88
                break;
89
            case 'name':
90
                $article->setName($value);
91
                break;
92
            case 'image':
93
                $lastUpdate = json_decode($connectAttribute->getLastUpdate(), true);
94
                $this->getImageImport()->importImagesForArticle(
95
                    array_diff($lastUpdate['image'], $lastUpdate['variantImages']),
96
                    $article
97
                );
98
                $this->getImageImport()->importImagesForDetail(
99
                    $lastUpdate['variantImages'],
100
                    $detail
101
                );
102
                break;
103
            case 'mainImage':
104
                $lastUpdate = json_decode($connectAttribute->getLastUpdate(), true);
105
                if (isset($lastUpdate['image'][0])) {
106
                    $this->getImageImport()->importMainImage(
107
                        $lastUpdate['image'][0], $article->getId()
108
                    );
109
                }
110
                break;
111
            case 'price':
112
                $netPrice = $value / (1 + ($article->getTax()->getTax()/100));
113
                $customerGroup = $this->getHelper()->getDefaultCustomerGroup();
114
115
                $detail->getPrices()->clear();
116
                $price = new Price();
117
                $price->fromArray([
118
                    'from' => 1,
119
                    'price' => $netPrice,
120
                    'basePrice' => $connectAttribute->getPurchasePrice(),
121
                    'customerGroup' => $customerGroup,
122
                    'article' => $article
123
                ]);
124
                $detail->setPrices([$price]);
125
                break;
126
        }
127
128
        if ($connectAttribute->getLastUpdateFlag() & $flag) {
129
            $connectAttribute->flipLastUpdateFlag($flag);
130
        }
131
        if ($type == 'image') {
132
            if ($connectAttribute->getLastUpdateFlag() & $updateFlagsByName['imageInitialImport']) {
133
                $connectAttribute->flipLastUpdateFlag($updateFlagsByName['imageInitialImport']);
134
            }
135
        }
136
137
        $this->getModelManager()->flush();
138
        $this->View()->assign('success', true);
139
    }
140
141
    /**
142
     * @return \ShopwarePlugins\Connect\Components\Helper
143
     */
144
    private function getHelper()
145
    {
146
        return $this->getConnectFactory()->getHelper();
147
    }
148
149
    /**
150
     * @return \ShopwarePlugins\Connect\Components\ConnectFactory
151
     */
152
    private function getConnectFactory()
153
    {
154
        if ($this->factory === null) {
155
            $this->factory = new \ShopwarePlugins\Connect\Components\ConnectFactory();
156
        }
157
158
        return $this->factory;
159
    }
160
161
    /**
162
     * @return \Shopware\Components\Model\ModelManager
163
     */
164
    public function getModelManager()
165
    {
166
        return Shopware()->Models();
167
    }
168
169
    /**
170
     * @return ImageImport
171
     */
172
    public function getImageImport()
173
    {
174
        return new ImageImport(
175
            $this->getModelManager(),
176
            $this->getHelper(),
177
            $this->get('thumbnail_manager'),
178
            new \ShopwarePlugins\Connect\Components\Logger(Shopware()->Db())
179
        );
180
    }
181
}
182