Debian Codegraph @

vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Sender/SocketSender.php

raw
1 <?php
2
3 namespace Liuggio\StatsdClient\Sender;
4
5 use Liuggio\StatsdClient\Exception\InvalidArgumentException;
6
7 Class SocketSender implements SenderInterface
8 {
9 private $port;
10 private $host;
11 private $protocol;
12
13 public function __construct($hostname = 'localhost', $port = 8126, $protocol = 'udp')
14 {
15 $this->host = $hostname;
16 $this->port = $port;
17
18 switch ($protocol) {
19 case 'udp':
20 $this->protocol = SOL_UDP;
21 break;
22 case 'tcp':
23 $this->protocol = SOL_TCP;
24 break;
25 default:
26 throw new InvalidArgumentException(sprintf('Use udp or tcp as protocol given %s', $protocol));
27 break;
28 }
29
30 }
31
32 /**
33 * {@inheritDoc}
34 */
35 public function open()
36 {
37 $fp = socket_create(AF_INET, SOCK_DGRAM, $this->getProtocol());
38
39 return $fp;
40 }
41
42 /**
43 * {@inheritDoc}
44 */
45 public function write($handle, $message, $length = null)
46 {
47 return socket_sendto($handle, $message, strlen($message), 0, $this->getHost(), $this->getPort());
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 public function close($handle)
54 {
55 socket_close($handle);
56 }
57
58
59 protected function setHost($host)
60 {
61 $this->host = $host;
62 }
63
64 protected function getHost()
65 {
66 return $this->host;
67 }
68
69 protected function setPort($port)
70 {
71 $this->port = $port;
72 }
73
74 protected function getPort()
75 {
76 return $this->port;
77 }
78
79 protected function setProtocol($protocol)
80 {
81 $this->protocol = $protocol;
82 }
83
84 protected function getProtocol()
85 {
86 return $this->protocol;
87 }
88 }