1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\Billing\Plan; |
23
|
|
|
|
24
|
|
|
use Parthenon\Billing\Exception\NoPlanFoundException; |
25
|
|
|
use Parthenon\Common\LoggerAwareTrait; |
26
|
|
|
|
27
|
|
|
class CachedPlanManager implements PlanManagerInterface |
28
|
|
|
{ |
29
|
|
|
use LoggerAwareTrait; |
30
|
|
|
|
31
|
|
|
public const REDIS_STORAGE_KEY = 'parthenon_plan'; |
32
|
|
|
private ?array $plans = null; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
private PlanManagerInterface $planManager, |
36
|
|
|
private \Redis $redis, |
37
|
|
|
) { |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getPlans(): array |
41
|
|
|
{ |
42
|
|
|
if (!isset($this->plans)) { |
43
|
|
|
$rawData = $this->redis->get(self::REDIS_STORAGE_KEY); |
44
|
|
|
|
45
|
|
|
if (!$rawData) { |
46
|
|
|
$this->getLogger()->debug('Fetching plans from original plan manager'); |
47
|
|
|
$this->plans = $this->planManager->getPlans(); |
48
|
|
|
$rawData = serialize($this->plans); |
49
|
|
|
$this->redis->set(self::REDIS_STORAGE_KEY, $rawData, 900); |
50
|
|
|
} else { |
51
|
|
|
$this->getLogger()->debug('Got the plans from cache'); |
52
|
|
|
$this->plans = unserialize($rawData); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->plans; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getPlanForUser(LimitedUserInterface $limitedUser): Plan |
60
|
|
|
{ |
61
|
|
|
return $this->getPlanByName($limitedUser->getPlanName()); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getPlanByName(string $planName): Plan |
65
|
|
|
{ |
66
|
|
|
$plans = $this->getPlans(); |
67
|
|
|
|
68
|
|
|
foreach ($plans as $plan) { |
69
|
|
|
if ($plan->getName() === $planName) { |
70
|
|
|
return $plan; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
throw new NoPlanFoundException(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|