Completed
Push — master ( 6e8858...85cc78 )
by Daniel
03:48
created

Factory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 63
ccs 0
cts 19
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSocketFactory() 0 8 2
A setSocketFactory() 0 6 1
A createSocket() 0 19 1
1
<?php
2
/**
3
 * This file is part of the Ssdp project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ssdp\Multicast;
9
10
use Socket\Raw\Factory as SocketFactory;
11
use Socket\Raw\Socket;
12
13
/**
14
 * Multicast factory class
15
 *
16
 * @package GravityMedia\Ssdp\Multicast
17
 */
18
class Factory
19
{
20
    /**
21
     * @var SocketFactory
22
     */
23
    protected $socketFactory;
24
25
    /**
26
     * Get socket factory
27
     *
28
     * @return SocketFactory
29
     */
30
    public function getSocketFactory()
31
    {
32
        if (null === $this->socketFactory) {
33
            $this->socketFactory = new SocketFactory();
34
        }
35
36
        return $this->socketFactory;
37
    }
38
39
    /**
40
     * Set socket factory
41
     *
42
     * @param SocketFactory $socketFactory
43
     *
44
     * @return $this
45
     */
46
    public function setSocketFactory(SocketFactory $socketFactory)
47
    {
48
        $this->socketFactory = $socketFactory;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Create socket
55
     *
56
     * @param string $address
57
     * @param int $timeout
58
     *
59
     * @return Socket
60
     */
61
    public function createSocket($address, $timeout = 0)
62
    {
63
        $socket = $this->getSocketFactory()->createUdp4();
64
65
        $socket
66
            ->setOption(SOL_SOCKET, SO_BROADCAST, 1)
67
            ->setOption(SOL_SOCKET, SO_SNDTIMEO, ['sec' => $timeout, 'usec' => 0])
68
            ->setOption(SOL_SOCKET, SO_RCVTIMEO, ['sec' => $timeout, 'usec' => 0]);
69
70
        $socket
71
            ->setOption(IPPROTO_IP, IP_MULTICAST_IF, 0)
72
            ->setOption(IPPROTO_IP, IP_MULTICAST_LOOP, 0)
73
            ->setOption(IPPROTO_IP, IP_MULTICAST_TTL, 1)
74
            ->setOption(IPPROTO_IP, MCAST_JOIN_GROUP, ['group' => $address, 'interface' => 0]);
75
76
        $socket->bind('0.0.0.0');
77
78
        return $socket;
79
    }
80
}
81