-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwagProductDiscountRepository.php
157 lines (123 loc) · 5.83 KB
/
SwagProductDiscountRepository.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
<?php
namespace SwagProductDiscount\Repository;
use Shopware\Api\Search\AggregationResult;
use Shopware\Api\Search\UuidSearchResult;
use Shopware\Api\Search\Criteria;
use Shopware\Api\Search\EntityAggregatorInterface;
use Shopware\Api\Search\EntitySearcherInterface;
use Shopware\Api\Read\EntityReaderInterface;
use Shopware\Api\RepositoryInterface;
use Shopware\Api\Write\EntityWriterInterface;
use Shopware\Api\Write\WriteContext;
use Shopware\Api\Write\GenericWrittenEvent;
use Shopware\Context\Struct\TranslationContext;
use SwagProductDiscount\Event\SwagProductDiscount\SwagProductDiscountSearchResultLoadedEvent;
use SwagProductDiscount\Event\SwagProductDiscount\SwagProductDiscountBasicLoadedEvent;
use SwagProductDiscount\Event\SwagProductDiscount\SwagProductDiscountAggregationResultLoadedEvent;
use SwagProductDiscount\Event\SwagProductDiscount\SwagProductDiscountUuidSearchResultLoadedEvent;
use SwagProductDiscount\Struct\SwagProductDiscountSearchResult;
use SwagProductDiscount\Definition\SwagProductDiscountDefinition;
use SwagProductDiscount\Collection\SwagProductDiscountBasicCollection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use SwagProductDiscount\Collection\SwagProductDiscountDetailCollection;
use SwagProductDiscount\Event\SwagProductDiscount\SwagProductDiscountDetailLoadedEvent;
class SwagProductDiscountRepository implements RepositoryInterface
{
/**
* @var EntityReaderInterface
*/
private $reader;
/**
* @var EntityWriterInterface
*/
private $writer;
/**
* @var EntitySearcherInterface
*/
private $searcher;
/**
* @var EntityAggregatorInterface
*/
private $aggregator;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
public function __construct(
EntityReaderInterface $reader,
EntityWriterInterface $writer,
EntitySearcherInterface $searcher,
EntityAggregatorInterface $aggregator,
EventDispatcherInterface $eventDispatcher
) {
$this->reader = $reader;
$this->writer = $writer;
$this->searcher = $searcher;
$this->aggregator = $aggregator;
$this->eventDispatcher = $eventDispatcher;
}
public function search(Criteria $criteria, TranslationContext $context): SwagProductDiscountSearchResult
{
$uuids = $this->searchUuids($criteria, $context);
$entities = $this->readBasic($uuids->getUuids(), $context);
$aggregations = null;
if ($criteria->getAggregations()) {
$aggregations = $this->aggregate($criteria, $context);
}
$result = SwagProductDiscountSearchResult::createFromResults($uuids, $entities, $aggregations);
$event = new SwagProductDiscountSearchResultLoadedEvent($result);
$this->eventDispatcher->dispatch($event->getName(), $event);
return $result;
}
public function aggregate(Criteria $criteria, TranslationContext $context): AggregationResult
{
$result = $this->aggregator->aggregate(SwagProductDiscountDefinition::class, $criteria, $context);
$event = new SwagProductDiscountAggregationResultLoadedEvent($result);
$this->eventDispatcher->dispatch($event->getName(), $event);
return $result;
}
public function searchUuids(Criteria $criteria, TranslationContext $context): UuidSearchResult
{
$result = $this->searcher->search(SwagProductDiscountDefinition::class, $criteria, $context);
$event = new SwagProductDiscountUuidSearchResultLoadedEvent($result);
$this->eventDispatcher->dispatch($event->getName(), $event);
return $result;
}
public function readBasic(array $uuids, TranslationContext $context): SwagProductDiscountBasicCollection
{
/** @var SwagProductDiscountBasicCollection $entities */
$entities = $this->reader->readBasic(SwagProductDiscountDefinition::class, $uuids, $context);
$event = new SwagProductDiscountBasicLoadedEvent($entities, $context);
$this->eventDispatcher->dispatch($event->getName(), $event);
return $entities;
}
public function readDetail(array $uuids, TranslationContext $context): SwagProductDiscountDetailCollection
{
/** @var SwagProductDiscountDetailCollection $entities */
$entities = $this->reader->readDetail(SwagProductDiscountDefinition::class, $uuids, $context);
$event = new SwagProductDiscountDetailLoadedEvent($entities, $context);
$this->eventDispatcher->dispatch($event->getName(), $event);
return $entities;
}
public function update(array $data, TranslationContext $context): GenericWrittenEvent
{
$affected = $this->writer->update(SwagProductDiscountDefinition::class, $data, WriteContext::createFromTranslationContext($context));
$event = GenericWrittenEvent::createFromWriterResult($affected, $context, []);
$this->eventDispatcher->dispatch(GenericWrittenEvent::NAME, $event);
return $event;
}
public function upsert(array $data, TranslationContext $context): GenericWrittenEvent
{
$affected = $this->writer->upsert(SwagProductDiscountDefinition::class, $data, WriteContext::createFromTranslationContext($context));
$event = GenericWrittenEvent::createFromWriterResult($affected, $context, []);
$this->eventDispatcher->dispatch(GenericWrittenEvent::NAME, $event);
return $event;
}
public function create(array $data, TranslationContext $context): GenericWrittenEvent
{
$affected = $this->writer->insert(SwagProductDiscountDefinition::class, $data, WriteContext::createFromTranslationContext($context));
$event = GenericWrittenEvent::createFromWriterResult($affected, $context, []);
$this->eventDispatcher->dispatch(GenericWrittenEvent::NAME, $event);
return $event;
}
}