UserlandDaemonFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createDaemon() 0 10 2
A createTcpDaemon() 0 11 2
A createDaemonFromStreamSocket() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPFastCGI\FastCGIDaemon\Driver\Userland;
6
7
use PHPFastCGI\FastCGIDaemon\DaemonInterface;
8
use PHPFastCGI\FastCGIDaemon\DaemonFactoryInterface;
9
use PHPFastCGI\FastCGIDaemon\DaemonOptions;
10
use PHPFastCGI\FastCGIDaemon\Driver\Userland\Connection\StreamSocketConnectionPool;
11
use PHPFastCGI\FastCGIDaemon\Driver\Userland\ConnectionHandler\ConnectionHandlerFactory;
12
use PHPFastCGI\FastCGIDaemon\KernelInterface;
13
14
/**
15
 * A factory class for instantiating UserlandDaemon objects.
16
 */
17
final class UserlandDaemonFactory implements DaemonFactoryInterface
18
{
19
    /**
20
     * Create a FastCGI daemon listening on file descriptor using the
21
     * userland FastCGI implementation.
22
     *
23
     * @param KernelInterface $kernel  The kernel to use for the daemon
24
     * @param DaemonOptions   $options The daemon configuration
25
     * @param int $fd file descriptor for listening defaults to FCGI_LISTENSOCK_FILENO
26
     *
27
     * @return UserlandDaemon
28
     *
29
     * @codeCoverageIgnore The FastCGI daemon
30
     */
31
    public function createDaemon(KernelInterface $kernel, DaemonOptions $options, int $fd = DaemonInterface::FCGI_LISTENSOCK_FILENO): DaemonInterface
32
    {
33
        $socket = fopen('php://fd/'.$fd, 'r');
34
35
        if (false === $socket) {
36
            throw new \RuntimeException('Could not open ' . $fd);
37
        }
38
39
        return $this->createDaemonFromStreamSocket($kernel, $options, $socket);
40
    }
41
42
    /**
43
     * Create a FastCGI daemon listening for TCP connections on a given address
44
     * using the userland FastCGI implementation. The default host is
45
     * 'localhost'.
46
     *
47
     * @param KernelInterface $kernel  The kernel to use for the daemon
48
     * @param DaemonOptions   $options The daemon configuration
49
     * @param string          $host    The host to bind to
50
     * @param int             $port    The port to bind to
51
     *
52
     * @return UserlandDaemon The FastCGI daemon
53
     *
54
     * @codeCoverageIgnore
55
     */
56
    public function createTcpDaemon(KernelInterface $kernel, DaemonOptions $options, string $host, int $port): DaemonInterface
57
    {
58
        $address = 'tcp://'.$host.':'.$port;
59
        $socket  = stream_socket_server($address);
60
61
        if (false === $socket) {
62
            throw new \RuntimeException('Could not create stream socket server on: '.$address);
63
        }
64
65
        return $this->createDaemonFromStreamSocket($kernel, $options, $socket);
66
    }
67
68
    /**
69
     * Create a FastCGI daemon from a stream socket which is configured for
70
     * accepting connections using the userland FastCGI implementation.
71
     *
72
     * @param KernelInterface $kernel  The kernel to use for the daemon
73
     * @param DaemonOptions   $options The daemon configuration
74
     * @param resource        $socket  The socket to accept connections from
75
     *
76
     * @return UserlandDaemon The FastCGI daemon
77
     */
78
    public function createDaemonFromStreamSocket(KernelInterface $kernel, DaemonOptions $options, $socket): DaemonInterface
79
    {
80
        $connectionPool           = new StreamSocketConnectionPool($socket);
81
        $connectionHandlerFactory = new ConnectionHandlerFactory();
82
83
        return new UserlandDaemon($kernel, $options, $connectionPool, $connectionHandlerFactory);
84
    }
85
}
86