Completed
Push — master ( 1cb211...95ea2a )
by Evgenij
03:22
created

PersistentClientSocket::createSocketResource()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 11
nc 2
nop 2
crap 4
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015, 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
11
namespace AsyncSockets\Socket;
12
13
use AsyncSockets\Exception\NetworkSocketException;
14
15
/**
16
 * Class PersistentClientSocket
17
 */
18
class PersistentClientSocket extends AbstractClientSocket
19
{
20
    /**
21
     * Key in php persistent storage to allow multiple persistent connections to the same host
22
     *
23
     * @var null|string
24
     */
25
    private $persistentKey;
26
27
    /**
28
     * PersistentClientSocket constructor.
29
     *
30
     * @param string|null $persistentKey Key in php persistent storage to allow multiple persistent
31
     *                                   connections to the same host [a-zA-Z0-9_-]
32
     */
33 18
    public function __construct($persistentKey = null)
34
    {
35 18
        parent::__construct();
36 18
        $this->persistentKey = $persistentKey;
37 18
    }
38
39
    /** {@inheritdoc} */
40 13
    protected function createSocketResource($address, $context)
41
    {
42 13
        $resource = stream_socket_client(
43 13
            $address . ($this->persistentKey ? '/' . $this->persistentKey : ''),
44 13
            $errno,
45 13
            $errstr,
46 13
            null,
47 13
            STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_PERSISTENT,
48
            $context
49 13
        );
50
51 13
        if ($errno || $resource === false) {
52 2
            throw new NetworkSocketException($this, $errstr, $errno);
53
        }
54
55 11
        return $resource;
56
    }
57
}
58