|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koality\ShopwarePlugin\Collector; |
|
4
|
|
|
|
|
5
|
|
|
use Koality\ShopwarePlugin\Formatter\Result; |
|
6
|
|
|
use Shopware\Core\Checkout\Order\OrderEntity; |
|
7
|
|
|
use Shopware\Core\Framework\Context; |
|
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; |
|
9
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
|
10
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class MinOrdersCollector |
|
14
|
|
|
* |
|
15
|
|
|
* @package Koality\ShopwarePlugin\Collector |
|
16
|
|
|
* |
|
17
|
|
|
* @author Nils Langner <[email protected]> |
|
18
|
|
|
* created 2020-12-28 |
|
19
|
|
|
*/ |
|
20
|
|
|
class CountOrdersCollector implements Collector |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $pluginConfig = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var EntityRepository |
|
29
|
|
|
*/ |
|
30
|
|
|
private $orderRepository; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Context |
|
34
|
|
|
*/ |
|
35
|
|
|
private $context; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* OpenCartsCollector constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $pluginConfig |
|
41
|
|
|
* @param Context $context |
|
42
|
|
|
* @param EntityRepository $orderRepository |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(array $pluginConfig, Context $context, EntityRepository $orderRepository) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->pluginConfig = $pluginConfig; |
|
47
|
|
|
$this->orderRepository = $orderRepository; |
|
48
|
|
|
$this->context = $context; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getResult() |
|
55
|
|
|
{ |
|
56
|
|
|
$salesThreshold = $this->getCurrentSalesThreshold(); |
|
57
|
|
|
|
|
58
|
|
|
$currentOrdersCount = $this->getLastHourOrderCount(); |
|
59
|
|
|
|
|
60
|
|
|
if ($currentOrdersCount < $salesThreshold) { |
|
61
|
|
|
$orderResult = new Result(Result::STATUS_FAIL, Result::KEY_ORDERS_TOO_FEW, 'There were too few orders within the last hour.'); |
|
62
|
|
|
} else { |
|
63
|
|
|
$orderResult = new Result(Result::STATUS_PASS, Result::KEY_ORDERS_TOO_FEW, 'There were enough orders within the last hour.'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$orderResult->setLimit($salesThreshold); |
|
67
|
|
|
$orderResult->setObservedValue($currentOrdersCount); |
|
68
|
|
|
$orderResult->setObservedValuePrecision(2); |
|
69
|
|
|
$orderResult->setObservedValueUnit('orders'); |
|
70
|
|
|
$orderResult->setLimitType(Result::LIMIT_TYPE_MIN); |
|
71
|
|
|
$orderResult->setType(Result::TYPE_TIME_SERIES_NUMERIC); |
|
72
|
|
|
|
|
73
|
|
|
return $orderResult; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return the sales threshold depending on the current time. |
|
78
|
|
|
* |
|
79
|
|
|
* @return int |
|
80
|
|
|
*/ |
|
81
|
|
|
private function getCurrentSalesThreshold() |
|
82
|
|
|
{ |
|
83
|
|
|
$config = $this->pluginConfig; |
|
84
|
|
|
|
|
85
|
|
|
if (array_key_exists('ordersPerHourRushHour', $config)) { |
|
86
|
|
|
$configValue = (int)(int)$config['ordersPerHourRushHour']; |
|
87
|
|
|
} else { |
|
88
|
|
|
$configValue = 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$currentWeekDay = date('w'); |
|
92
|
|
|
$isWeekend = ($currentWeekDay == 0 || $currentWeekDay == 6); |
|
93
|
|
|
|
|
94
|
|
|
$allowRushHour = !($isWeekend && !$config['includeWeekends']); |
|
95
|
|
|
|
|
96
|
|
|
if ($allowRushHour && array_key_exists('rushHourBegin', $config) && array_key_exists('rushHourEnd', $config)) { |
|
97
|
|
|
$beginHour = (int)substr($config['rushHourBegin'], 0, 2) . substr($config['rushHourBegin'], 3, 2); |
|
98
|
|
|
$endHour = (int)substr($config['rushHourEnd'], 0, 2) . substr($config['rushHourEnd'], 3, 2); |
|
99
|
|
|
|
|
100
|
|
|
$currentTime = (int)date('Hi'); |
|
101
|
|
|
|
|
102
|
|
|
if ($currentTime < $endHour && $currentTime > $beginHour) { |
|
103
|
|
|
return $configValue; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
return $configValue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the number of orders within the last hour. |
|
113
|
|
|
* |
|
114
|
|
|
* @return int |
|
115
|
|
|
*/ |
|
116
|
|
|
private function getLastHourOrderCount() |
|
117
|
|
|
{ |
|
118
|
|
|
$criteria = new Criteria(); |
|
119
|
|
|
$criteria->addFilter(new RangeFilter('createdAt', [ |
|
120
|
|
|
RangeFilter::GTE => date('Y-m-d H:i:s', strtotime('- 1 hour')) |
|
121
|
|
|
])); |
|
122
|
|
|
|
|
123
|
|
|
$orderRepository = $this->orderRepository; |
|
124
|
|
|
|
|
125
|
|
|
/** @var OrderEntity[] $orders */ |
|
126
|
|
|
$orders = $orderRepository->search($criteria, $this->context); |
|
127
|
|
|
|
|
128
|
|
|
return count($orders); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|