summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SocketSender.php
blob: c319f0776336062556f6a58702c40f61ea354eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

namespace Liuggio\StatsdClient\Sender;

use Liuggio\StatsdClient\Exception\InvalidArgumentException;

Class SocketSender implements SenderInterface
{
    private $port;
    private $host;
    private $protocol;

    public function __construct($hostname = 'localhost', $port = 8126, $protocol = 'udp')
    {
        $this->host = $hostname;
        $this->port = $port;

        switch ($protocol) {
            case 'udp':
                $this->protocol = SOL_UDP;
                break;
            case 'tcp':
                $this->protocol = SOL_TCP;
                break;
            default:
                throw new InvalidArgumentException(sprintf('Use udp or tcp as protocol given %s', $protocol));
                break;
        }

    }

    /**
     * {@inheritDoc}
     */
    public function open()
    {
        $fp = socket_create(AF_INET, SOCK_DGRAM, $this->getProtocol());

        return $fp;
    }

    /**
     * {@inheritDoc}
     */
    public function write($handle, $message, $length = null)
    {
        return socket_sendto($handle, $message, strlen($message), 0, $this->getHost(), $this->getPort());
    }

    /**
     * {@inheritDoc}
     */
    public function close($handle)
    {
        socket_close($handle);
    }


    protected function setHost($host)
    {
        $this->host = $host;
    }

    protected function getHost()
    {
        return $this->host;
    }

    protected function setPort($port)
    {
        $this->port = $port;
    }

    protected function getPort()
    {
        return $this->port;
    }

    protected function setProtocol($protocol)
    {
        $this->protocol = $protocol;
    }

    protected function getProtocol()
    {
        return $this->protocol;
    }
}