Completed
Branch 0.4-dev (79cc15)
by Evgenij
03:32
created

AsyncSocketFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 99
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
B createSocket() 0 19 6
B createRequestExecutor() 0 23 5
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
11
namespace AsyncSockets\Socket;
12
13
use AsyncSockets\Configuration\Configuration;
14
use AsyncSockets\RequestExecutor\LibEventRequestExecutor;
15
use AsyncSockets\RequestExecutor\NativeRequestExecutor;
16
use AsyncSockets\RequestExecutor\Pipeline\BaseStageFactory;
17
use AsyncSockets\RequestExecutor\Pipeline\PipelineFactory;
18
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
19
20
/**
21
 * Class AsyncSocketFactory
22
 *
23
 * @api
24
 */
25
class AsyncSocketFactory
26
{
27
    /**
28
     * Create client socket
29
     */
30
    const SOCKET_CLIENT = 'client';
31
32
    /**
33
     * Create server socket
34
     */
35
    const SOCKET_SERVER = 'server';
36
37
    /**
38
     * Boolean flag whether it is persistent socket, applicable only for SOCKET_CLIENT type
39
     */
40
    const SOCKET_OPTION_IS_PERSISTENT = 'soIsPersistent';
41
42
    /**
43
     * Key in php storage to allow multiple persistent connections to the same host [a-zA-Z0-9_-]
44
     */
45
    const SOCKET_OPTION_PERSISTENT_KEY = 'soPersistentKey';
46
47
    /**
48
     * Default configuration for this factory
49
     *
50
     * @var Configuration
51
     */
52
    private $configuration;
53
54
    /**
55
     * AsyncSocketFactory constructor.
56
     *
57
     * @param Configuration $configuration Default configuration for this factory
58
     */
59 6
    public function __construct(Configuration $configuration = null)
60
    {
61 6
        $this->configuration = $configuration ?: new Configuration();
62 6
    }
63
64
    /**
65
     * Create socket client
66
     *
67
     * @param string      $type Socket type to create, one of SOCKET_* consts
68
     * @param array       $options  $flags Flags with socket settings, see SOCKET_OPTION_* consts
69
     *
70
     * @return SocketInterface
71
     * @api
72
     */
73 4
    public function createSocket($type = self::SOCKET_CLIENT, array $options = [])
74
    {
75
        switch ($type) {
76 4
            case self::SOCKET_CLIENT:
77 2
                $isPersistent  = isset($options[ self::SOCKET_OPTION_IS_PERSISTENT ]) &&
78 2
                                 $options[ self::SOCKET_OPTION_IS_PERSISTENT ];
79 2
                $persistentKey = isset($options[ self::SOCKET_OPTION_PERSISTENT_KEY ]) ?
80
                    $options[ self::SOCKET_OPTION_PERSISTENT_KEY ] :
81 2
                    null;
82
83 2
                return $isPersistent ?
84 1
                    new PersistentClientSocket($persistentKey) :
85 2
                    new ClientSocket();
86 2
            case self::SOCKET_SERVER:
87 1
                return new ServerSocket();
88
            default:
89 1
                throw new \InvalidArgumentException("Unexpected type {$type} used in " . __FUNCTION__);
90
        }
91
    }
92
93
    /**
94
     * Create RequestExecutor object
95
     *
96
     * @return RequestExecutorInterface
97
     *
98
     * @api
99
     */
100 2
    public function createRequestExecutor()
101
    {
102 2
        foreach ($this->configuration->getPreferredEngines() as $engine) {
103
            switch ($engine) {
104 2
                case 'libevent':
105 1
                    if (extension_loaded('libevent')) {
106
                        return new LibEventRequestExecutor(new BaseStageFactory(), $this->configuration);
107
                    }
108 1
                    break;
109 2
                case 'native':
110 1
                    return new NativeRequestExecutor(
111 1
                        new PipelineFactory(
112 1
                            new BaseStageFactory()
113
                        ),
114 2
                        $this->configuration
115
                    );
116
            }
117
        }
118
119 1
        throw new \InvalidArgumentException(
120 1
            'Provided configuration does not contain any supported RequestExecutor engine.'
121
        );
122
    }
123
}
124