RedisFactory::getConnectionMethod()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Redis\Business\Redis;
15
16
use Micro\Plugin\Redis\Configuration\ClientOptionsConfigurationInterface;
17
use Micro\Plugin\Redis\Configuration\RedisClientConfigurationInterface;
18
19
/**
20
 * @author Stanislau Komar <[email protected]>
21
 */
22
class RedisFactory implements RedisFactoryInterface
23
{
24 2
    public function create(RedisClientConfigurationInterface $clientConfiguration): \Redis
25
    {
26 2
        $redis = new \Redis();
27
28 2
        $this->initialize($redis, $clientConfiguration);
29
30
        return $redis;
31
    }
32
33
    /**
34
     * @throws \RedisException
35
     */
36 2
    protected function initialize(\Redis $redis, RedisClientConfigurationInterface $configuration): void
37
    {
38 2
        $connectionMethod = $this->getConnectionMethod($configuration);
39
40 2
        if (RedisClientConfigurationInterface::CONNECTION_TYPE_UNIX === $configuration->connectionType()) {
41
            $redis->{$connectionMethod}($configuration->host());
42
43
            return;
44
        }
45
46 2
        \call_user_func(
47 2
            [$redis, $connectionMethod], // @phpstan-ignore-line
48 2
            $configuration->host(),
49 2
            $configuration->port(),
50 2
            $configuration->connectionTimeout(),
51 2
            $this->getPersistentId($configuration),
52 2
            $configuration->retryInterval(),
53 2
            $configuration->readTimeout(),
54 2
        );
55
56
        $this->setOptions($redis, $configuration->clientOptionsConfiguration());
57
    }
58
59
    /**
60
     * @throws \RedisException
61
     */
62
    protected function setOptions(\Redis $redis, ClientOptionsConfigurationInterface $configuration): void
63
    {
64
        $redis->setOption(\Redis::OPT_SERIALIZER, $this->getRedisOptionValue($configuration->serializer()));
65
        $redis->setOption(\Redis::OPT_PREFIX, $configuration->prefix());
66
    }
67
68
    protected function getRedisOptionValue(string $redisOption): int
69
    {
70
        return \constant("Redis::$redisOption");
71
    }
72
73 2
    protected function getConnectionMethod(RedisClientConfigurationInterface $configuration): string
74
    {
75 2
        return $configuration->reuseConnection() ? 'pconnect' : 'connect';
76
    }
77
78 2
    protected function getPersistentId(RedisClientConfigurationInterface $configuration): ?string
79
    {
80 2
        return $configuration->reuseConnection() ? $configuration->name() : null;
81
    }
82
}
83