Completed
Push — master ( 452f3b...8b11af )
by Changwan
04:35
created

MemcachedServiceProvider::addServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\DI\Providers\Pecl;
3
4
use Memcached;
5
use Wandu\Config\Contracts\Config;
6
use Wandu\DI\ContainerInterface;
7
use Wandu\DI\ServiceProviderInterface;
8
9
class MemcachedServiceProvider implements ServiceProviderInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function register(ContainerInterface $app)
15
    {
16
        $app->bind(Memcached::class)->after(function(Memcached $memcached) use ($app) {
17
            $config = $app->get(Config::class);
18
            foreach ($config->get('pecl.memcached.servers', []) as $server) {
19
                $this->addServer($memcached, $server);
20
            }
21
            if ($server = $config->get('pecl.memcached.server')) {
22
                $this->addServer($memcached, $server);
23
            }
24
        });
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function boot(ContainerInterface $app)
31
    {
32
    }
33
    
34
    private function addServer(Memcached $memcached, array $server)
35
    {
36
        $memcached->addServer(
37
            $server['host'] ?? '127.0.0.1',
38
            $server['port'] ?? 11211,
39
            $server['weight'] ?? 0
40
        );
41
    }
42
}
43