Passed
Pull Request — master (#444)
by Alejandro
08:10
created

RedisFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 10
ccs 7
cts 7
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Cache;
5
6
use Predis\Client as PredisClient;
7
use Psr\Container\ContainerInterface;
8
9
use function array_shift;
10
use function count;
11
use function is_array;
12
use function is_string;
13
14
class RedisFactory
15
{
16
    public const SERVICE_NAME = 'Shlinkio\Shlink\Common\Cache\Redis';
17
18 5
    public function __invoke(ContainerInterface $container): PredisClient
19
    {
20 5
        $redisConfig = $container->get('config')['redis'] ?? [];
21 5
        $servers = $redisConfig['servers'] ?? [];
22
23 5
        if (is_array($servers) && count($servers) === 1) {
24 1
            $servers = array_shift($servers);
25
        }
26
27 5
        $options = is_string($servers) || count($servers) < 1 ? null : ['cluster' => 'redis'];
28 5
        return new PredisClient($servers, $options);
29
    }
30
}
31