-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
91 lines (73 loc) · 2.13 KB
/
helper.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
<?php
namespace app;
use Memcached;
class helper {
const cacheKey = 'inUse';
const cacheServer = 'memcached';
const cachePort = 11211;
const acceptedPayment = [1, 5, 10];
protected Memcached $cache;
function __construct()
{
$this->cache = new Memcached();
$this->cache->addServer(self::cacheServer, self::cachePort);
}
function isScriptInUse(): bool
{
return $this->cache->get(self::cacheKey);
}
function lockScript(): void
{
$this->cache->set(self::cacheKey, true, 10);
}
function unlockScript(string $message = null): void
{
if ($message) {
echo $message . PHP_EOL;
}
$this->cache->set(self::cacheKey, false);
}
function validateArguments(array $options)
{
if (\key_exists('reset', $options)) {
$this->resetDefault();
echo 'We reset Memcached' . PHP_EOL;
exit;
}
if (!\key_exists('id', $options)) {
$this->unlockScript('ERROR - Please select a product id --id [1-4]');
exit;
}
if (!\key_exists('coin', $options)) {
$this->unlockScript('ERROR - Wrong payment input!');
echo 'example: --coin=\'{"1":2, "5":1}\' => 2 coins of value 1 and 1 coin value 5 total 7' . PHP_EOL;
exit;
}
$coin = json_decode($options['coin']);
if (!$coin instanceof \stdClass) {
$this->unlockScript('ERROR - Wrong payment input!');
echo 'example: --coin=\'{"1":2, "5":1}\' => 2 coins of value 1 and 1 coin value 5 total 7' . PHP_EOL;
exit;
}
}
function getCache(): Memcached
{
return $this->cache;
}
function resetDefault()
{
$this->cache->flush();
}
function calculateTotal(string $coins): int
{
$coins = \json_decode($coins);
$total = 0;
foreach ($coins as $coin => $amount) {
$coin = (int)$coin;
if (\in_array($coin, self::acceptedPayment)) {
$total += $coin * $amount;
}
}
return $total;
}
}