Passed
Push — master ( 8f00fc...54b7bc )
by Eugene
05:22
created

ensure_server_version_at_least()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
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
use Tarantool\Client\Client;
15
use Tarantool\Client\Connection\StreamConnection;
16
use Tarantool\Client\Handler\DefaultHandler;
17
use Tarantool\Client\Packer\PackerFactory;
18
use Tarantool\Client\Packer\PurePacker;
19
use Tarantool\Client\Tests\Integration\ExamplesTest;
20
21
return require __DIR__.'/../vendor/autoload.php';
22
23
function create_client() : Client
24
{
25
    $connection = isset($_SERVER['argv'][1])
26
        ? StreamConnection::create($_SERVER['argv'][1])
27
        : StreamConnection::createTcp();
28
29
    return new Client(new DefaultHandler($connection, PackerFactory::create()));
30
}
31
32
function server_version_at_least(string $version, Client $client) : bool
33
{
34
    $connection = $client->getHandler()->getConnection();
35
    if (!$greeting = $connection->open()) {
36
        throw new \RuntimeException('Failed to retrieve server version.');
37
    }
38
39
    return version_compare($greeting->getServerVersion(), $version, '>=');
40
}
41
42
function ensure_server_version_at_least(string $version, Client $client) : void
43
{
44
    if (server_version_at_least($version, $client)) {
45
        return;
46
    }
47
48
    printf('Tarantool version >= %s is required to run "%s".%s', $version, $_SERVER['SCRIPT_FILENAME'], PHP_EOL);
49
    exit(ExamplesTest::EXIT_CODE_SKIP);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
50
}
51
52
function ensure_extension(string $name) : void
53
{
54
    if (extension_loaded($name)) {
55
        return;
56
    }
57
58
    printf('"PHP extension "%s" is required to run "%s".%s', $name, $_SERVER['SCRIPT_FILENAME'], PHP_EOL);
59
    exit(ExamplesTest::EXIT_CODE_SKIP);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
60
}
61
62
function ensure_pure_packer(Client $client) : void
63
{
64
    $packer = $client->getHandler()->getPacker();
65
    if ($packer instanceof PurePacker) {
66
        return;
67
    }
68
69
    printf('Client needs to be configured to use pure packer to run "%s".%s', $_SERVER['SCRIPT_FILENAME'], PHP_EOL);
70
    exit(ExamplesTest::EXIT_CODE_SKIP);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
71
}
72