Completed
Branch master (00332a)
by Eugene
05:11
created

FakeServerBuilder::setTtl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Tests\Integration\FakeServer;
15
16
use Tarantool\Client\Tests\Integration\FakeServer\Handler\ChainHandler;
17
use Tarantool\Client\Tests\Integration\FakeServer\Handler\Handler;
18
use Tarantool\Client\Tests\Integration\FakeServer\Handler\NoopHandler;
19
20
final class FakeServerBuilder
21
{
22
    private $handler;
23
    private $uri = 'tcp://0.0.0.0:8000';
24
    private $ttl = 5;
25
    private $logFile;
26
27
    public function __construct(Handler $handler)
28
    {
29
        $this->handler = $handler;
30
        $this->logFile = sys_get_temp_dir().'/tarantool_php_client_fake_server.log';
31
    }
32
33
    public function setUri(string $uri) : self
34
    {
35
        $this->uri = $uri;
36
37
        return $this;
38
    }
39
40
    public function setTtl(int $ttl) : self
41
    {
42
        $this->ttl = $ttl;
43
44
        return $this;
45
    }
46
47
    public function setLogFile(string $logFile) : self
48
    {
49
        $this->logFile = $logFile;
50
51
        return $this;
52
    }
53
54
    public function getCommand() : string
55
    {
56
        return sprintf(
57
            'php %s/fake_server.php \
58
                --handler=%s \
59
                --uri=%s \
60
                --ttl=%d \
61
            >> %s 2>&1 &',
62
            __DIR__,
63
            escapeshellarg(base64_encode(serialize($this->handler))),
64
            escapeshellarg($this->uri),
65
            $this->ttl,
66
            escapeshellarg($this->logFile)
67
        );
68
    }
69
70
    public function start() : void
71
    {
72
        exec($this->getCommand(), $output, $result);
73
        if (0 !== $result) {
74
            throw new \RuntimeException("Unable to start fake server ($this->uri)");
75
        }
76
77
        $stopTime = time() + 5;
78
        while (time() < $stopTime) {
79
            if ($stream = @stream_socket_client($this->uri)) {
80
                fclose($stream);
81
82
                return;
83
            }
84
            usleep(100);
85
        }
86
87
        throw new \RuntimeException("Unable to connect to fake server ($this->uri)");
88
    }
89
90
    public static function create(Handler ...$handlers) : self
91
    {
92
        if (!$handlers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $handlers of type Tarantool\Client\Tests\I...erver\Handler\Handler[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
            return new self(new NoopHandler());
94
        }
95
96
        return count($handlers) > 1
97
            ? new self(new ChainHandler($handlers))
98
            : new self($handlers[0]);
99
    }
100
}
101