DatagramServer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A recv() 0 7 2
1
<?php
2
3
namespace Helix\Socket;
4
5
/**
6
 * Receives datagrams.
7
 */
8
class DatagramServer extends AbstractServer {
9
10
    /**
11
     * `SOCK_DGRAM`
12
     *
13
     * @return int
14
     */
15
    final public static function getType (): int {
16
        return SOCK_DGRAM;
17
    }
18
19
    /**
20
     * Receives data from a peer.
21
     *
22
     * @see https://php.net/socket_recvfrom
23
     *
24
     * @param int $length Maximum length to read.
25
     * @param int $flags `MSG_*`
26
     * @param string $name Assigned the peer's address, or Unix socket file path.
27
     * @param int $port Assigned the peer's port, or `0` for Unix sockets.
28
     * @return string
29
     * @throws SocketError
30
     */
31
    public function recv (int $length, int $flags = 0, string &$name = '', int &$port = 0): string {
32
        $count = @socket_recvfrom($this->resource, $data, $length, $flags, $name, $port);
33
        if ($count === false) {
34
            throw new SocketError($this->resource, SOCKET_EOPNOTSUPP);
35
        }
36
        return (string)$data; // cast needed, will be null if 0 bytes are read
37
    }
38
39
}