Passed
Push — main ( 6f6d96...2c5ee0 )
by Iain
18:19
created

CachedSubscriptionProvider::refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
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\Subscription;
23
24
use Parthenon\Billing\Entity\CustomerInterface;
25
use Parthenon\Billing\Entity\Subscription;
26
use Parthenon\Common\LoggerAwareTrait;
27
28
class CachedSubscriptionProvider implements SubscriptionProviderInterface
29
{
30
    use LoggerAwareTrait;
31
32
    public const REDIS_STORAGE_SUBSCRIPTION_KEY = 'parthenon_subscription_%s';
33
    public const REDIS_STORAGE_SUBSCRIPTIONS_KEY = 'parthenon_subscriptions_%s';
34
    private array $subscriptions = [];
35
    private array $subscription = [];
36
37
    public function __construct(
38
        private SubscriptionProviderInterface $subscriptionProvider,
39
        private \Redis $redis,
40
    ) {
41
    }
42
43
    public function getSubscription(string $id): Subscription
44
    {
45
        if (isset($this->subscription[$id])) {
46
            return $this->subscription[$id];
47
        }
48
49
        $key = sprintf(self::REDIS_STORAGE_SUBSCRIPTION_KEY, $id);
50
        $rawData = $this->redis->get($key);
51
52
        if (!$rawData) {
53
            $this->getLogger()->debug('Fetching subscription from original subscription provider');
54
            $this->subscription[$id] = $this->subscriptionProvider->getSubscription($id);
55
            $rawData = serialize($this->subscription[$id]);
56
            $this->redis->set($key, $rawData);
57
        } else {
58
            $this->getLogger()->debug('Got the subscription from cache');
59
            $this->subscription[$id] = unserialize($rawData);
60
        }
61
62
        return $this->subscription[$id];
63
    }
64
65
    public function getSubscriptionsForCustomer(CustomerInterface $customer): array
66
    {
67
        $id = (string) $customer->getId();
68
69
        if (isset($this->subscriptions[$id])) {
70
            return $this->subscriptions[$id];
71
        }
72
73
        $key = sprintf(self::REDIS_STORAGE_SUBSCRIPTIONS_KEY, $id);
74
        $rawData = $this->redis->get($key);
75
76
        if (!$rawData) {
77
            $this->getLogger()->debug('Fetching subscriptions from original subscription provider');
78
            $this->subscriptions[$id] = $this->subscriptionProvider->getSubscriptionsForCustomer($customer);
79
            $rawData = serialize($this->subscriptions[$id]);
80
            $this->redis->set($key, $rawData, 900);
81
        } else {
82
            $this->getLogger()->debug('Got the subscriptions from cache');
83
            $this->subscriptions[$id] = unserialize($rawData);
84
        }
85
86
        return $this->subscriptions[$id];
87
    }
88
89
    public function refresh(Subscription $subscription): Subscription
90
    {
91
        $key = sprintf(self::REDIS_STORAGE_SUBSCRIPTION_KEY, $subscription->getId());
92
        $this->redis->del($key);
93
        $this->subscription = [];
94
95
        return $this->getSubscription($subscription->getId());
96
    }
97
}
98