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

PersistentClientSocket   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createSocketResource() 0 17 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