-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
AttributeGroupRepository.php
183 lines (162 loc) · 6.99 KB
/
AttributeGroupRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
declare(strict_types=1);
namespace PrestaShop\PrestaShop\Adapter\AttributeGroup\Repository;
use AttributeGroup;
use Doctrine\DBAL\Connection;
use PrestaShop\PrestaShop\Core\Domain\Product\AttributeGroup\ValueObject\AttributeGroupId;
use PrestaShop\PrestaShop\Core\Domain\Shop\Exception\InvalidShopConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Shop\Exception\ShopAssociationNotFound;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopId;
use PrestaShop\PrestaShop\Core\Repository\AbstractMultiShopObjectModelRepository;
class AttributeGroupRepository extends AbstractMultiShopObjectModelRepository
{
/**
* @var Connection
*/
private $connection;
/**
* @var string
*/
private $dbPrefix;
public function __construct(
Connection $connection,
string $dbPrefix
) {
$this->connection = $connection;
$this->dbPrefix = $dbPrefix;
}
/**
* @param ShopConstraint $shopConstraint
* @param AttributeGroupId[] $attributeGroupIds get only certain attribute groups (e.g. when need to get only certain combinations attributes groups)
*
* @return array<int, AttributeGroup> array key is the id of attribute group
*/
public function getAttributeGroups(ShopConstraint $shopConstraint, array $attributeGroupIds = []): array
{
if ($shopConstraint->getShopGroupId()) {
throw new InvalidShopConstraintException('Shop Group constraint is not supported');
}
$shopIdValue = $shopConstraint->getShopId() ? $shopConstraint->getShopId()->getValue() : null;
$qb = $this->connection->createQueryBuilder()
->select('ag.*, agl.*')
->from($this->dbPrefix . 'attribute_group', 'ag')
->innerJoin(
'ag',
$this->dbPrefix . 'attribute_group_lang',
'agl',
'ag.id_attribute_group = agl.id_attribute_group'
)
->orderBy('ag.position', 'ASC')
;
if (!empty($attributeGroupIds)) {
$attributeGroupIdValues = array_map(static function (AttributeGroupId $attributeGroupId): int {
return $attributeGroupId->getValue();
}, $attributeGroupIds);
$qb->andWhere($qb->expr()->in('ag.id_attribute_group', ':attributeGroupIds'))
->setParameter('attributeGroupIds', $attributeGroupIdValues, Connection::PARAM_INT_ARRAY)
;
}
if ($shopIdValue) {
$qb
->innerJoin(
'ag',
$this->dbPrefix . 'attribute_group_shop',
'ags',
'ag.id_attribute_group = ags.id_attribute_group'
)
->andWhere('ags.id_shop = :shopId')
->setParameter('shopId', $shopIdValue)
;
}
$results = $qb->execute()->fetchAllAssociative();
if (!$results) {
return [];
}
$attributeGroups = [];
foreach ($results as $result) {
$attributeGroupId = (int) $result['id_attribute_group'];
$langId = (int) $result['id_lang'];
if (isset($attributeGroups[$attributeGroupId])) {
$attributeGroup = $attributeGroups[$attributeGroupId];
} else {
$attributeGroup = new AttributeGroup();
$attributeGroups[$attributeGroupId] = $attributeGroup;
}
$attributeGroup->id = $attributeGroupId;
$attributeGroup->is_color_group = (bool) $result['is_color_group'];
$attributeGroup->group_type = (string) $result['group_type'];
$attributeGroup->position = (int) $result['position'];
$attributeGroup->name[$langId] = (string) $result['name'];
$attributeGroup->public_name[$langId] = (string) $result['public_name'];
}
return $attributeGroups;
}
/**
* Asserts that attribute groups exists in all the provided shops.
* If at least one of them is missing in any shop, it throws exception.
*
* @param AttributeGroupId[] $attributeGroupIds
* @param ShopId[] $shopIds
*
* @throws ShopAssociationNotFound
*/
public function assertExistsInEveryShop(array $attributeGroupIds, array $shopIds): void
{
$attributeGroupIdValues = array_map(static function (AttributeGroupId $attributeGroupId): int {
return $attributeGroupId->getValue();
}, $attributeGroupIds);
$shopIdValues = array_map(static function (ShopId $shopId): int {
return $shopId->getValue();
}, $shopIds);
$qb = $this->connection->createQueryBuilder();
$results = $qb
->select('ags.id_shop, ag.id_attribute_group')
->from($this->dbPrefix . 'attribute_group', 'ag')
->innerJoin(
'ag',
$this->dbPrefix . 'attribute_group_shop',
'ags',
'ag.id_attribute_group = ags.id_attribute_group AND ags.id_shop IN (:shopIds)'
)
->andWhere($qb->expr()->in('ag.id_attribute_group', ':attributeGroupIds'))
->setParameter('shopIds', $shopIdValues, Connection::PARAM_INT_ARRAY)
->setParameter('attributeGroupIds', $attributeGroupIdValues, Connection::PARAM_INT_ARRAY)
->execute()
->fetchAllAssociative()
;
$attributeGroupShops = [];
foreach ($results as $result) {
$attributeGroupShops[(int) $result['id_attribute_group']][] = (int) $result['id_shop'];
}
foreach ($attributeGroupIdValues as $attributeGroupIdValue) {
if (!isset($attributeGroupShops[$attributeGroupIdValue]) || $attributeGroupShops[$attributeGroupIdValue] !== $shopIdValues) {
throw new ShopAssociationNotFound('Provided attribute groups does not exist in every shop');
}
}
}
}