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

RandomReplicationCluster::makeOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Drivers\RandomReplication;
17
18
use Phpfastcache\Cluster\Drivers\MasterSlaveReplication\MasterSlaveReplicationCluster;
19
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
20
use ReflectionException;
21
use ReflectionMethod;
22
23
24
/**
25
 * Class MasterSlaveReplicationCluster
26
 * @package Phpfastcache\Cluster\Drivers\MasterSlaveReplication
27
 */
28
class RandomReplicationCluster extends MasterSlaveReplicationCluster
29
{
30
    /**
31
     * RandomReplicationCluster constructor.
32
     * @param string $clusterName
33
     * @param ExtendedCacheItemPoolInterface ...$driverPools
34
     * @throws ReflectionException
35
     */
36
    public function __construct(string $clusterName, ExtendedCacheItemPoolInterface ...$driverPools)
37
    {
38
        (new ReflectionMethod(\get_parent_class(\get_parent_class($this)), __FUNCTION__))
39
            ->invoke($this, $clusterName, ...$driverPools);
40
        $randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)];
41
42
        $this->eventManager->dispatch(
43
            'CacheReplicationRandomPoolChosen',
44
            $this,
45
            $randomPool
46
        );
47
48
        $this->clusterPools = [$randomPool];
49
    }
50
51
    /**
52
     * @param callable $operation
53
     * @return mixed
54
     */
55
    protected function makeOperation(callable $operation)
56
    {
57
        return $operation($this->getMasterPool());
58
    }
59
}
60