LocalUdpClientFactory::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 8.8571
cc 5
eloc 7
nc 16
nop 2
1
<?php
2
3
namespace Werkspot\Bundle\StatsdBundle\Service;
4
5
use Domnikl\Statsd\Connection\UdpSocket;
6
use Werkspot\Bundle\StatsdBundle\Client\StatsdClient;
7
use Werkspot\Bundle\StatsdBundle\Client\StatsdClientInterface;
8
9
class LocalUdpClientFactory implements StatsdClientFactoryInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $applicationPrefix;
15
16
    /**
17
     * @var UdpSocket
18
     */
19
    private $connection;
20
21
    /**
22
     * @param string $applicationPrefix
23
     * @param array $connectionOptions
24
     */
25
    public function __construct($applicationPrefix = '', array $connectionOptions = [])
26
    {
27
        $host = isset($connectionOptions['host']) ? $connectionOptions['host'] : '127.0.0.1';
28
        $port = isset($connectionOptions['port']) ? $connectionOptions['port'] : '8125';
29
        $timeout = isset($connectionOptions['timeout']) ? $connectionOptions['timeout'] : null;
30
        $persistent = isset($connectionOptions['persistent']) ? $connectionOptions['persistent'] : false;
31
32
        $this->connection = new UdpSocket($host, $port, $timeout, $persistent);
33
        $this->applicationPrefix = $applicationPrefix;
34
    }
35
36
    /**
37
     * @param string $namespace
38
     *
39
     * @return StatsdClientInterface
40
     */
41
    public function getClient($namespace)
42
    {
43
        $namespace = $this->applicationPrefix . $namespace;
44
        return new StatsdClient($this->connection, $namespace);
45
    }
46
}
47