Completed
Pull Request — master (#399)
by Christian
03:10
created

AttributeRepository::getLocalArticleCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 9.4285
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 Shopware\CustomModels\Connect;
9
10
use Shopware\Components\Model\ModelRepository;
11
use Doctrine\DBAL\DBALException;
12
use Doctrine\DBAL\Connection;
13
14
/**
15
 * Class AttributeRepository
16
 * @package Shopware\CustomModels\Connect
17
 */
18
class AttributeRepository extends ModelRepository
19
{
20
    public function findRemoteArticleAttributes()
21
    {
22
        return $this->getRemoteArticleAttributesQuery()->getResult();
23
    }
24
25
    public function getRemoteArticleAttributesQuery()
26
    {
27
        return $this->getRemoteArticleAttributesQueryBuilder()->getQuery();
28
    }
29
30
    public function getRemoteArticleAttributesQueryBuilder()
31
    {
32
        $builder = $this->createQueryBuilder('a');
33
        $builder->select('a');
34
        $builder->where('a.shopId IS NOT NULL')
35
                ->andWhere('a.category IS NOT NULL');
36
37
        return $builder;
38
    }
39
40
    /**
41
     * @param array $status
42
     * @return mixed
43
     */
44
    public function countStatus(array $status)
45
    {
46
        $builder = $this->createQueryBuilder('at');
47
        $builder
48
            ->select($builder->expr()->count('at.id'))
49
            ->where('at.exportStatus IN (:status)')
50
            ->setParameter(
51
                'status',
52
                $status,
53
                \Doctrine\DBAL\Connection::PARAM_STR_ARRAY
54
            );
55
56
        return $builder->getQuery()->getSingleScalarResult();
57
    }
58
59
    /**
60
     * Resets the exported items
61
     */
62
    public function resetExportedItemsStatus()
63
    {
64
        $builder = $this->_em->createQueryBuilder();
65
        $builder->update('Shopware\CustomModels\Connect\Attribute', 'a')
66
            ->set('a.exportStatus', '(:newStatus)')
67
            ->set('a.exported', 0)
68
            ->set('a.revision', '(:revision)')
69
            ->where('a.exportStatus IN (:status)')
70
            ->setParameter('newStatus', null)
71
            ->setParameter('revision', null)
72
            ->setParameter(
73
                'status',
74
                [Attribute::STATUS_INSERT, Attribute::STATUS_UPDATE, Attribute::STATUS_SYNCED],
75
                \Doctrine\DBAL\Connection::PARAM_STR_ARRAY
76
            );
77
78
        $builder->getQuery()->execute();
79
    }
80
81
    /**
82
     * List with all changed products which are coming from Connect
83
     *
84
     * @param int $start
85
     * @param int $limit
86
     * @param array $order
87
     * @return \Doctrine\DBAL\Query\QueryBuilder
88
     */
89
    public function getChangedProducts($start, $limit, array $order = [])
90
    {
91
        $builder = $this->_em->getConnection()->createQueryBuilder();
92
        $builder->from('s_plugin_connect_items', 'at');
93
        $builder->join('at', 's_articles', 'a', 'at.article_id = a.id');
94
        $builder->join('at', 's_articles_details', 'd', 'at.article_detail_id = d.id');
95
        $builder->join('at', 's_articles_attributes', 'cad', 'at.article_detail_id = cad.articleDetailsId');
96
        $builder->leftJoin('at', 's_articles_prices', 'p', "p.from = 1 AND p.pricegroup = 'EK'");
97
        $builder->leftJoin('a', 's_articles_supplier', 's', 'a.supplierID = s.id');
98
        $builder->leftJoin('a', 's_core_tax', 't', 'a.taxID = t.id');
99
100
        $builder->select([
101
            'at.last_update as lastUpdate',
102
            'at.last_update_flag as lastUpdateFlag',
103
            'a.id as articleId',
104
            'd.id',
105
            'd.ordernumber as number',
106
            'd.inStock as inStock',
107
            'cad.connect_product_description as additionalDescription',
108
            'a.name as name',
109
            'a.description',
110
            'a.description_long as descriptionLong',
111
            's.name as supplier',
112
            'a.active as active',
113
            't.tax as tax',
114
            'p.price * (100 + t.tax) / 100 as price',
115
            'at.category'
116
        ]);
117
118
        $builder->where('at.shop_id IS NOT NULL')
119
            ->andWHere('at.last_update_flag IS NOT NULL')
120
            ->andWHere('at.last_update_flag > 0');
121
122
        if (isset($order[0]) && isset($order[0]['property']) && isset($order[0]['direction'])) {
123
            $builder->addOrderBy($order[0]['property'], $order[0]['direction']);
124
        }
125
126
        $builder->setFirstResult($start);
127
        $builder->setMaxResults($limit);
128
129
        return $builder;
130
    }
131
132
    /**
133
     * @param int $offset
134
     * @param int $batchSize
135
     * @return array
136
     */
137
    public function findAllSourceIds($offset, $batchSize)
138
    {
139
        $customProductsTableExists = $this->hasCustomProductsTable();
140
141
        // main variants should be collected first, because they
142
        // should be exported first. Connect uses first variant product with an unknown groupId as main one.
143
        $builder = $this->_em->getConnection()->createQueryBuilder();
144
        $builder->select('spci.source_id')
145
            ->from('s_plugin_connect_items', 'spci')
146
            ->leftJoin('spci', 's_articles_details', 'sad', 'spci.article_detail_id = sad.id')
147
            ->where('sad.kind IN (1,2) AND spci.shop_id IS NULL')
148
            ->orderBy('sad.kind', 'ASC')
149
            ->addOrderBy('spci.source_id', 'ASC')
150
            ->setFirstResult($offset)
151
            ->setMaxResults($batchSize);
152
153
        if ($customProductsTableExists) {
154
            $builder->leftJoin('spci', 's_plugin_custom_products_template_product_relation', 'spcptpr', 'spci.article_id = spcptpr.article_id')
155
                ->andWhere('spcptpr.template_id IS NULL');
156
        }
157
158
        return $builder->execute()->fetchAll(\PDO::FETCH_COLUMN);
159
    }
160
161
    public function getLocalArticleCount()
162
    {
163
        $conn = $this->_em->getConnection();
164
165
        $sql = 'SELECT COUNT(article_id) FROM s_plugin_connect_items';
166
167
        $query = $conn->query($sql);
168
169
        return $query->fetchColumn();
170
    }
171
172
    /**
173
     * @param array $articleIds
174
     * @param int $kind
175
     * @return array
176
     */
177
    public function findSourceIds(array $articleIds, $kind)
178
    {
179
        $customProductsTableExists = $this->hasCustomProductsTable();
180
181
        // main variants should be collected first, because they
182
        // should be exported first. Connect uses first variant product with an unknown groupId as main one.
183
        $builder = $this->_em->getConnection()->createQueryBuilder();
184
        $builder->select('spci.source_id')
185
            ->from('s_plugin_connect_items', 'spci')
186
            ->rightJoin('spci', 's_articles_details', 'sad', 'spci.article_detail_id = sad.id')
187
            ->where('sad.articleID IN (:articleIds) AND sad.kind = :kind AND spci.shop_id IS NULL')
188
            ->setParameter(':articleIds', $articleIds, Connection::PARAM_INT_ARRAY)
189
            ->setParameter('kind', $kind, \PDO::PARAM_INT);
190
191
        if ($customProductsTableExists) {
192
            $builder->leftJoin('spci', 's_plugin_custom_products_template_product_relation', 'spcptpr', 'spci.article_id = spcptpr.article_id')
193
                ->andWhere('spcptpr.template_id IS NULL');
194
        }
195
196
        return $builder->execute()->fetchAll(\PDO::FETCH_COLUMN);
197
    }
198
199
    /**
200
     * @return bool
201
     */
202
    private function hasCustomProductsTable()
203
    {
204
        $customProductsTableExists = false;
205
        try {
206
            $builder = $this->_em->getConnection()->createQueryBuilder();
207
            $builder->select('id');
208
            $builder->from('s_plugin_custom_products_template');
209
            $builder->setMaxResults(1);
210
            $builder->execute()->fetch();
211
212
            $customProductsTableExists = true;
213
        } catch (DBALException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\DBALException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
214
            // ignore it
215
            // custom products is not installed
216
        }
217
218
        return $customProductsTableExists;
219
    }
220
}
221