Completed
Push — master ( 3d22df...95226d )
by Georges
22s queued 12s
created

Driver   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A makeOperation() 0 3 1
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\Driver as MasterSlaveReplicationDriver;
19
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
20
use Phpfastcache\Event\EventManagerInterface;
21
use ReflectionException;
22
use ReflectionMethod;
23
24
class Driver extends MasterSlaveReplicationDriver
25
{
26
    /**
27
     * RandomReplicationCluster constructor.
28
     * @param string $clusterName
29
     * @param ExtendedCacheItemPoolInterface ...$driverPools
30
     * @throws ReflectionException
31
     */
32
    public function __construct(string $clusterName, EventManagerInterface $em, ExtendedCacheItemPoolInterface ...$driverPools)
33
    {
34
        (new ReflectionMethod(\get_parent_class(\get_parent_class($this)), __FUNCTION__))
35
            ->invoke($this, $clusterName, $em, ...$driverPools);
36
        $randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)];
37
38
        $this->eventManager->dispatch(
39
            'CacheReplicationRandomPoolChosen',
40
            $this,
41
            $randomPool
42
        );
43
44
        $this->clusterPools = [$randomPool];
45
    }
46
47
    /**
48
     * @param callable $operation
49
     * @return mixed
50
     */
51
    protected function makeOperation(callable $operation)
52
    {
53
        return $operation($this->getMasterPool());
54
    }
55
}
56