Completed
Push — master ( c97103...9edfc5 )
by Evgenij
03:08
created

ServerSocket::isClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\Socket;
11
12
use AsyncSockets\Exception\ConnectionException;
13
use AsyncSockets\Socket\Io\DatagramServerIo;
14
use AsyncSockets\Socket\Io\StreamedServerIo;
15
16
/**
17
 * Class ServerSocket
18
 */
19
class ServerSocket extends AbstractSocket
20
{
21
    /** {@inheritdoc} */
22 5
    protected function createSocketResource($address, $context)
23
    {
24 5
        $type     = $this->getSocketScheme($address);
25 5
        $resource = stream_socket_server(
26 5
            $address,
27 5
            $errno,
28 5
            $errstr,
29 5
            $this->getServerFlagsByType($type),
30
            $context
31 5
        );
32
33 5
        if ($errno || $resource === false) {
34 2
            throw new ConnectionException($this, $errstr, $errno);
35
        }
36
37 3
        return $resource;
38
    }
39
40
    /** {@inheritdoc} */
41 3
    protected function createIoInterface($type, $address)
42
    {
43
        switch ($type) {
44 3
            case self::SOCKET_TYPE_UNIX:
45
                return new StreamedServerIo($this);
46 3
            case self::SOCKET_TYPE_TCP:
47 2
                return new StreamedServerIo($this);
48 1
            case self::SOCKET_TYPE_UDG:
49
                return new DatagramServerIo($this, true);
50 1
            case self::SOCKET_TYPE_UDP:
51
                return new DatagramServerIo($this, false);
52 1
            default:
53 1
                throw new \LogicException("Unsupported socket resource type {$type}");
54 1
        }
55
    }
56
57
    /**
58
     * Return socket scheme
59
     *
60
     * @param string $address Address in form scheme://host:port
61
     *
62
     * @return string|null
63
     */
64 5
    private function getSocketScheme($address)
65
    {
66 5
        $pos = strpos($address, '://');
67 5
        if ($pos === false) {
68 1
            return null;
69
        }
70
71 4
        return substr($address, 0, $pos);
72
    }
73
74
    /**
75
     * Return flags for connection
76
     *
77
     * @param string $scheme Socket type being created
78
     *
79
     * @return int
80
     */
81 5
    private function getServerFlagsByType($scheme)
82
    {
83
        $connectionLessMap = [
84 5
            'udp' => 1,
85 5
            'udg' => 1,
86 5
        ];
87
88 5
        return isset($connectionLessMap[ $scheme ]) ?
89 5
            STREAM_SERVER_BIND :
90 5
            STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 3
    public function isServer()
97
    {
98 3
        return true;
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104 1
    public function isClient()
105
    {
106 1
        return false;
107
    }
108
}
109