1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koality\ShopwarePlugin\Collector; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\DBALException; |
7
|
|
|
use Koality\ShopwarePlugin\Formatter\Result; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class OpenCartsCollector |
11
|
|
|
* |
12
|
|
|
* @package Koality\ShopwarePlugin\Collector |
13
|
|
|
* |
14
|
|
|
* @author Nils Langner <[email protected]> |
15
|
|
|
* created 2020-12-28 |
16
|
|
|
*/ |
17
|
|
|
class OpenCartsCollector implements Collector |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $pluginConfig = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Connection |
26
|
|
|
*/ |
27
|
|
|
private $connection; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* OpenCartsCollector constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $pluginConfig |
33
|
|
|
* @param Connection $connection |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $pluginConfig, Connection $connection) |
36
|
|
|
{ |
37
|
|
|
$this->pluginConfig = $pluginConfig; |
38
|
|
|
$this->connection = $connection; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
* @throws DBALException |
44
|
|
|
*/ |
45
|
|
|
public function getResult(): Result |
46
|
|
|
{ |
47
|
|
|
$cartCount = $this->getOpenCartCount(); |
48
|
|
|
|
49
|
|
|
if (array_key_exists('openCarts', $this->pluginConfig)) { |
50
|
|
|
$maxCartCount = $this->pluginConfig['openCarts']; |
51
|
|
|
} else { |
52
|
|
|
$maxCartCount = 1000; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($cartCount > $maxCartCount) { |
56
|
|
|
$cartResult = new Result(Result::STATUS_FAIL, Result::KEY_CARTS_OPEN_TOO_MANY, 'There are too many open carts at the moment.'); |
57
|
|
|
} else { |
58
|
|
|
$cartResult = new Result(Result::STATUS_PASS, Result::KEY_CARTS_OPEN_TOO_MANY, 'There are not too many open carts at the moment.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$cartResult->setLimit($maxCartCount); |
62
|
|
|
$cartResult->setObservedValue($cartCount); |
63
|
|
|
$cartResult->setObservedValueUnit('carts'); |
64
|
|
|
$cartResult->setObservedValuePrecision(0); |
65
|
|
|
$cartResult->setLimitType(Result::LIMIT_TYPE_MAX); |
66
|
|
|
$cartResult->setType(Result::TYPE_TIME_SERIES_NUMERIC); |
67
|
|
|
|
68
|
|
|
return $cartResult; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the number of open carts. |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
* |
76
|
|
|
* @throws DBALException |
77
|
|
|
* |
78
|
|
|
* @todo duplicated code with NewsletterSubscriptionCollector |
79
|
|
|
*/ |
80
|
|
|
private function getOpenCartCount(): int |
81
|
|
|
{ |
82
|
|
|
$sql = 'SELECT count(*) FROM cart WHERE created_at >= ? AND created_at < ?'; |
83
|
|
|
|
84
|
|
|
$statement = $this->connection->executeQuery($sql, [ |
85
|
|
|
date('Y.m.d', strtotime('-1 hour')), |
86
|
|
|
date('Y.m.d') |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
$count = $statement->fetchColumn(); |
|
|
|
|
90
|
|
|
|
91
|
|
|
if ($count === false) { |
92
|
|
|
return -1; |
93
|
|
|
} else { |
94
|
|
|
return (int)$count; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.