Passed
Push — main ( 131a53...56e1da )
by Iain
05:07
created

CachedSubscriptionProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscriptionsForCustomer() 0 22 3
A getSubscription() 0 20 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2024 Iain Cambridge
7
 *
8
 *     This program is free software: you can redistribute it and/or modify
9
 *     it under the terms of the GNU General Public License as published by
10
 *     the Free Software Foundation, either version 3 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 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->subscriptions[$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);
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