-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPromotionStorage.php
More file actions
172 lines (147 loc) · 5.36 KB
/
PromotionStorage.php
File metadata and controls
172 lines (147 loc) · 5.36 KB
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
<?php
namespace Drupal\commerce_promotion;
use Drupal\commerce\CommerceContentEntityStorage;
use Drupal\commerce_order\Entity\OrderTypeInterface;
use Drupal\commerce_store\Entity\StoreInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Defines the promotion storage.
*/
class PromotionStorage extends CommerceContentEntityStorage implements PromotionStorageInterface {
/**
* The usage.
*
* @var \Drupal\commerce_promotion\PromotionUsageInterface
*/
protected $usage;
/**
* The time.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs a new PromotionStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Database\Connection $database
* The database connection to be used.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\commerce_promotion\PromotionUsageInterface $usage
* The usage.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
*/
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, PromotionUsageInterface $usage, TimeInterface $time) {
parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager, $event_dispatcher);
$this->usage = $usage;
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('database'),
$container->get('entity.manager'),
$container->get('cache.entity'),
$container->get('language_manager'),
$container->get('event_dispatcher'),
$container->get('commerce_promotion.usage'),
$container->get('datetime.time')
);
}
/**
* {@inheritdoc}
*/
public function loadAvailable(OrderTypeInterface $order_type, StoreInterface $store) {
$today = gmdate('Y-m-d', $this->time->getRequestTime());
$query = $this->getQuery();
$or_condition = $query->orConditionGroup()
->condition('end_date', $today, '>=')
->notExists('end_date', $today);
$query
->condition('stores', [$store->id()], 'IN')
->condition('order_types', [$order_type->id()], 'IN')
->condition('start_date', $today, '<=')
->condition('status', TRUE)
->condition($or_condition);
// Only load promotions without coupons. Promotions with coupons are loaded
// coupon-first in a different process.
$query->notExists('coupons');
$result = $query->execute();
if (empty($result)) {
return [];
}
$promotions = $this->loadMultiple($result);
// Remove any promotions that have hit their usage limit.
$promotions_with_usage_limits = array_filter($promotions, function ($promotion) {
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
return !empty($promotion->getUsageLimit());
});
$usages = $this->usage->getUsageMultiple($promotions_with_usage_limits);
foreach ($promotions_with_usage_limits as $promotion_id => $promotion) {
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
if ($promotion->getUsageLimit() <= $usages[$promotion_id]) {
unset($promotions[$promotion_id]);
}
}
// Sort the remaining promotions.
uasort($promotions, [$this->entityType->getClass(), 'sort']);
return $promotions;
}
/**
* {@inheritdoc}
*/
public function loadExpired() {
$query = $this->getQuery();
$query
->condition('end_date', gmdate('Y-m-d'), '<')
->condition('status', TRUE);
$result = $query->execute();
if (empty($result)) {
return [];
}
return $this->loadMultiple($result);
}
/**
* {@inheritdoc}
*/
public function loadMaxedUsage() {
$query = $this->getQuery();
$query
->condition('usage_limit', 1, '>=')
->condition('status', TRUE);
$result = $query->execute();
if (empty($result)) {
return [];
}
$promotions = $this->loadMultiple($result);
$maxed_promotions = [];
// Get an array of each promotion's use count.
$promotion_uses = $this->usage->getUsageMultiple($promotions);
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
foreach ($promotions as $promotion) {
if ($promotion_uses[$promotion->id()] >= $promotion->getUsageLimit()) {
$maxed_promotions[] = $promotion;
}
}
return $maxed_promotions;
}
}