Completed
Push — master ( be9660...707803 )
by Georges
16s queued 13s
created

ClusterAggregator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 138
rs 10
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A disaggregateDriver() 0 11 3
A getClusterAggregatorName() 0 3 1
A aggregateDriver() 0 15 4
A __construct() 0 15 4
A aggregateNewDriver() 0 7 2
A getCluster() 0 22 3
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author  Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Cluster;
17
18
use Exception;
19
use Phpfastcache\CacheManager;
20
use Phpfastcache\Config\ConfigurationOption;
21
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
22
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
23
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
24
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
25
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
26
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
27
use ReflectionException;
28
use stdClass;
29
30
/**
31
 * Class ClusterAggregator
32
 *
33
 * @package Phpfastcache\Cluster
34
 */
35
class ClusterAggregator implements AggregatorInterface
36
{
37
38
    protected $driverPools;
39
40
    /**
41
     * @var ClusterPoolInterface
42
     */
43
    protected $cluster;
44
45
    /**
46
     * @var string
47
     */
48
    protected $clusterAggregatorName;
49
50
    /**
51
     * ClusterAggregator constructor.
52
     * @param string $clusterAggregatorName
53
     * @param AggregatablePoolInterface ...$driverPools
54
     * @throws PhpfastcacheLogicException
55
     */
56
    public function __construct(string $clusterAggregatorName = '', AggregatablePoolInterface ...$driverPools)
57
    {
58
        $clusterAggregatorName = trim($clusterAggregatorName);
59
        if (empty($clusterAggregatorName)) {
60
            try {
61
                $clusterAggregatorName = 'cluster_' . \bin2hex(\random_bytes(15));
62
            } catch (Exception $e) {
63
                $clusterAggregatorName = 'cluster_' . \str_shuffle(\spl_object_hash(new stdClass()));
64
            }
65
        }
66
67
        $this->clusterAggregatorName = $clusterAggregatorName;
68
69
        foreach ($driverPools as $driverPool) {
70
            $this->aggregateDriver($driverPool);
71
        }
72
    }
73
74
    /**
75
     * @param AggregatablePoolInterface $driverPool
76
     *
77
     * @throws PhpfastcacheLogicException
78
     */
79
    public function aggregateDriver(AggregatablePoolInterface $driverPool): void
80
    {
81
        if ($this->cluster) {
82
            throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.');
83
        }
84
85
        $splHash = \spl_object_hash($driverPool);
86
        if (!isset($this->driverPools[$splHash])) {
87
            if ($driverPool instanceof ClusterPoolInterface) {
88
                throw new PhpfastcacheLogicException('Recursive cluster aggregation is not allowed !');
89
            }
90
91
            $this->driverPools[$splHash] = $driverPool;
92
        } else {
93
            throw new PhpfastcacheLogicException('This pool has been already aggregated !');
94
        }
95
    }
96
97
    /**
98
     * @param string $driverName
99
     * @param ConfigurationOption|null $driverConfig
100
     * @throws PhpfastcacheDriverCheckException
101
     * @throws PhpfastcacheDriverException
102
     * @throws PhpfastcacheDriverNotFoundException
103
     * @throws PhpfastcacheInvalidArgumentException
104
     * @throws PhpfastcacheInvalidConfigurationException
105
     * @throws PhpfastcacheLogicException
106
     * @throws ReflectionException
107
     */
108
    public function aggregateNewDriver(string $driverName, ConfigurationOption $driverConfig = null): void
109
    {
110
        if ($this->cluster) {
111
            throw new PhpfastcacheLogicException('The cluster has been already build, cannot aggregate more pools.');
112
        }
113
        $this->aggregateDriver(
114
            CacheManager::getInstance($driverName, $driverConfig)
0 ignored issues
show
Bug introduced by
Phpfastcache\CacheManage...verName, $driverConfig) of type Phpfastcache\Core\Pool\E...dCacheItemPoolInterface is incompatible with the type Phpfastcache\Cluster\AggregatablePoolInterface expected by parameter $driverPool of Phpfastcache\Cluster\Clu...ator::aggregateDriver(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            /** @scrutinizer ignore-type */ CacheManager::getInstance($driverName, $driverConfig)
Loading history...
115
        );
116
    }
117
118
    /**
119
     * @param AggregatablePoolInterface $driverPool
120
     *
121
     * @throws PhpfastcacheLogicException
122
     */
123
    public function disaggregateDriver(AggregatablePoolInterface $driverPool): void
124
    {
125
        if ($this->cluster) {
126
            throw new PhpfastcacheLogicException('The cluster has been already build, cannot disaggregate pools.');
127
        }
128
129
        $splHash = \spl_object_hash($driverPool);
130
        if (isset($this->driverPools[$splHash])) {
131
            unset($this->driverPools[$splHash]);
132
        } else {
133
            throw new PhpfastcacheLogicException('This pool was not aggregated !');
134
        }
135
    }
136
137
    /**
138
     * @param int $strategy
139
     *
140
     * @return ClusterPoolInterface
141
     * @throws PhpfastcacheInvalidArgumentException
142
     */
143
    public function getCluster(int $strategy = AggregatorInterface::STRATEGY_FULL_REPLICATION): ClusterPoolInterface
144
    {
145
        if (isset(ClusterPoolAbstract::STRATEGY[$strategy])) {
146
            if (!$this->cluster) {
147
                $clusterClass = ClusterPoolAbstract::STRATEGY[$strategy];
148
                $this->cluster = new $clusterClass(
149
                    $this->getClusterAggregatorName(),
150
                    ...\array_values($this->driverPools)
151
                );
152
153
                /**
154
                 * @eventName CacheClusterBuilt
155
                 * @param $clusterAggregator AggregatorInterface
156
                 * @param $cluster ClusterPoolInterface
157
                 */
158
                $this->cluster->getEventManager()->dispatch('CacheClusterBuilt', $this, $this->cluster);
159
            }
160
        } else {
161
            throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy');
162
        }
163
164
        return $this->cluster;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getClusterAggregatorName(): string
171
    {
172
        return $this->clusterAggregatorName;
173
    }
174
}
175