RedisServiceProvider::register()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 2
b 0
f 0
1
<?php
2
3
namespace Ps2alerts\Api\ServiceProvider;
4
5
use League\Container\ServiceProvider\AbstractServiceProvider;
6
use Ps2alerts\Api\Contract\ConfigAwareInterface;
7
use Ps2alerts\Api\Contract\ConfigAwareTrait;
8
use Predis\Client;
9
use Ps2alerts\Api\Utility\RedisUtility;
10
11
class RedisServiceProvider extends AbstractServiceProvider implements
12
    ConfigAwareInterface
13
{
14
    use ConfigAwareTrait;
15
    /**
16
     * @var array
17
     */
18
    protected $provides = [
19
        'redis',
20
        'RedisUtility'
21
    ];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function register()
27
    {
28
        $this->getContainer()->add('redis', function () {
29
            $redisConfig = $this->getContainer()->get('config')['redis'];
30
31
            $args = [
32
                'host'     => $redisConfig['host'],
33
                'port'     => $redisConfig['port'],
34
                'database' => intval($redisConfig['db']),
35
                'scheme'   => 'tcp',
36
            ];
37
38
            if (! empty($redisConfig['pass'])) {
39
                $args['password'] = $redisConfig['pass'];
40
            }
41
42
            $client = new Client($args);
43
44
            return $client;
45
        });
46
47
        $this->getContainer()->share('RedisUtility', function() {
48
            return new RedisUtility();
49
        });
50
    }
51
}
52