-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttributeCollector.php
154 lines (127 loc) · 5.17 KB
/
AttributeCollector.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
<?php
declare(strict_types=1);
/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/
namespace CoreShop\Component\Variant;
use CoreShop\Component\Variant\Event\VariantAvailabilityEvent;
use CoreShop\Component\Variant\Model\AttributeGroupInterface;
use CoreShop\Component\Variant\Model\AttributeInterface;
use CoreShop\Component\Variant\Model\ProductVariantAwareInterface;
use CoreShop\Component\Variant\Model\Resolved\ResolvedAttribute;
use CoreShop\Component\Variant\Model\Resolved\ResolvedAttributeGroup;
use CoreShop\Component\Variant\Model\Resolved\ResolvedIndex;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class AttributeCollector implements AttributeCollectorInterface
{
public function __construct(
private EventDispatcherInterface $eventDispatcher,
) {
}
/**
* @return ResolvedAttributeGroup[]
*/
public function getAttributesFromVariants(ProductVariantAwareInterface $product, bool $showInList = false): array
{
if (AbstractObject::OBJECT_TYPE_VARIANT === $product->getType()) {
$variants = [$product];
} else {
$variants = $product->getVariants();
}
return $this->getAttributes($variants, $showInList);
}
/**
* @return ResolvedAttributeGroup[]
*/
public function getAttributes(array $products, bool $showInList = false): array
{
$resolvedGroups = [];
foreach ($products as $product) {
$event = $this->eventDispatcher->dispatch(new VariantAvailabilityEvent($product), 'coreshop.attribute.collector.preCondition');
if (!$event->isConditionMet()) {
continue;
}
/**
* @var AttributeInterface $attribute
*/
foreach ($product->getAttributes() as $attribute) {
/**
* @var AttributeGroupInterface|null $attributeGroup
*/
$attributeGroup = $attribute->getAttributeGroup();
if (!$attributeGroup) {
continue;
}
if ($showInList && !$attributeGroup->getShowInList()) {
continue;
}
if (!isset($resolvedGroups[$attributeGroup->getId()])) {
$group = new ResolvedAttributeGroup();
$group->setGroup($attributeGroup);
$group->setType(get_class($attribute));
$resolvedGroups[$attributeGroup->getId()] = $group;
} else {
$group = $resolvedGroups[$attributeGroup->getId()];
}
$resolvedAttribute = new ResolvedAttribute($attribute);
if (!$group->hasAttribute($resolvedAttribute)) {
$group->addAttribute($resolvedAttribute);
} else {
$resolvedAttribute = $group->getAttribute($attribute->getId());
}
$resolvedAttribute->addProduct($product);
$group->addAttribute($resolvedAttribute);
}
}
usort($resolvedGroups, static fn (ResolvedAttributeGroup $a, ResolvedAttributeGroup $b) => $a->getGroup()->getSorting() <=> $b->getGroup()->getSorting());
return $resolvedGroups;
}
/**
* @return ResolvedAttributeGroup[]
*/
public function getAttributesFromObject(ProductVariantAwareInterface $product, bool $showInList = false): array
{
if (AbstractObject::OBJECT_TYPE_VARIANT === $product->getType()) {
$product = $product->getVariantParent();
}
return $this->getAttributes($product->getVariants(), $showInList);
}
public function getIndex(ProductVariantAwareInterface $product)
{
$index = [];
if ($product->getType() === AbstractObject::OBJECT_TYPE_VARIANT) {
$product = $product->getVariantParent();
}
$variants = $product->getVariants();
foreach ($variants as $variant) {
if (!$variant instanceof ProductVariantAwareInterface) {
continue;
}
$attributeGroups = $this->getAttributes([$variant], false);
$indexElement = new ResolvedIndex();
/**
* @psalm-var Concrete&ProductVariantAwareInterface $variant
*/
$indexElement->setUrl($variant->getClass()->getLinkGenerator()->generate($variant));
foreach ($attributeGroups as $attributeGroup) {
$attribute = $attributeGroup->getAttributes();
$attribute = reset($attribute);
$indexElement->addAttribute($attributeGroup);
}
$index[$variant->getId()] = $indexElement;
}
return $index;
}
}