Passed
Pull Request — master (#838)
by Georges
02:41
created

Driver::makeOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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