Completed
Push — master ( 5415e6...23faee )
by Sven
09:51
created

getRemoteArticleAttributesQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 array $articleIds
134
     * @param int $kind
135
     * @return array
136
     */
137
    public function findSourceIds(array $articleIds, $kind)
138
    {
139
        $customProductsTableExists = false;
140
        try {
141
            $builder = $this->_em->getConnection()->createQueryBuilder();
142
            $builder->select('id');
143
            $builder->from('s_plugin_custom_products_template');
144
            $builder->setMaxResults(1);
145
            $builder->execute()->fetch();
146
147
            $customProductsTableExists = true;
148
        } 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...
149
            // ignore it
150
            // custom products is not installed
151
        }
152
153
        // main variants should be collected first, because they
154
        // should be exported first. Connect uses first variant product with an unknown groupId as main one.
155
        $builder = $this->_em->getConnection()->createQueryBuilder();
156
        $builder->select('spci.source_id')
157
            ->from('s_plugin_connect_items', 'spci')
158
            ->rightJoin('spci', 's_articles_details', 'sad', 'spci.article_detail_id = sad.id')
159
            ->where('sad.articleID IN (:articleIds) AND sad.kind = :kind AND spci.shop_id IS NULL')
160
            ->setParameter(':articleIds', $articleIds, Connection::PARAM_INT_ARRAY)
161
            ->setParameter('kind', $kind, \PDO::PARAM_INT);
162
163
        if ($customProductsTableExists) {
164
            $builder->leftJoin('spci', 's_plugin_custom_products_template_product_relation', 'spcptpr', 'spci.article_id = spcptpr.article_id')
165
                ->andWhere('spcptpr.template_id IS NULL');
166
        }
167
168
        return $builder->execute()->fetchAll(\PDO::FETCH_COLUMN);
169
    }
170
}
171