Passed
Pull Request — master (#17)
by BENOIT
02:04
created

get_client_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\MercurePHP;
4
5
use Lcobucci\JWT\Signer;
6
use Symfony\Component\Console\Input\InputInterface;
7 2
use Psr\Http\Message\UriInterface;
8 2
use Ramsey\Uuid\Uuid;
9
10 2
const CLIENT_NAMESPACE = '530344d8-a802-11ea-bb37-0242ac130002';
11 2
12
function nullify($input)
13
{
14
    if (!\is_scalar($input)) {
15
        return $input;
16
    }
17
    if ((string) $input === '') {
18
        return null;
19
    }
20
21
    return $input;
22
}
23
24
function get_signer(string $algorithm): Signer
25
{
26
    $map = [
27
        'HS256' => new Signer\Hmac\Sha256(),
28
        'RS512' => new Signer\Rsa\Sha512(),
29
    ];
30
31
    if (!isset($map[$algorithm])) {
32
        throw new \InvalidArgumentException(\sprintf('Invalid algorithm %s.', $algorithm));
33
    }
34
35
    return $map[$algorithm];
36
}
37
38
function without_nullish_values(array $array): array
39
{
40
    return \array_filter(
41
        $array,
42
        fn($value) => null !== nullify($value) && false !== $value
43
    );
44
}
45
46
function get_options_from_input(InputInterface $input): array
47
{
48
    return \array_filter(
49
        $input->getOptions(),
50
        fn($value) => null !== nullify($value) && false !== $value
51
    );
52
}
53
54
function get_client_id(string $remoteHost, int $remotePort): string
55
{
56
    return (string) Uuid::uuid5(CLIENT_NAMESPACE, \sprintf('%s:%d', $remoteHost, $remotePort));
57
}
58