AParams   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 64
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getPort() 0 3 2
A setRequest() 0 5 1
A getTimeout() 0 3 2
A getHost() 0 4 2
A setTarget() 0 6 1
A getProtocolVersion() 0 3 1
A getSchema() 0 11 2
1
<?php
2
3
namespace kalanis\RemoteRequest\Connection\Params;
4
5
6
use kalanis\RemoteRequest\Interfaces;
7
8
9
/**
10
 * Class AParams
11
 * @package kalanis\RemoteRequest\Connection\Params
12
 * Schemes for creating a connection
13
 * Define known schemes for access remote resource via php internal calls
14
 * @link https://www.php.net/manual/en/wrappers.php
15
 */
16
abstract class AParams implements Interfaces\IConnectionParams
17
{
18
    protected string $host = '';
19
    protected int $port = 1;
20
    protected float $timeout = 30.0;
21
22
    abstract protected function getSchemaType(): string;
23
24 12
    public function setTarget(?string $host = null, ?int $port = null, ?float $timeout = null): self
25
    {
26 12
        $this->host = $host ?? $this->host;
27 12
        $this->port = $port ?? $this->port;
28 12
        $this->timeout = $timeout ?? $this->timeout;
29 12
        return $this;
30
    }
31
32 1
    public function setRequest(Interfaces\ITarget $request): self
33
    {
34 1
        $this->host = $request->getHost();
35 1
        $this->port = $request->getPort() ?? $this->port;
36 1
        return $this;
37
    }
38
39
    /**
40
     * Generate correct hostname
41
     * This method updates IPv6 address into form that is usable by sockets
42
     */
43 13
    public function getHost(): string
44
    {
45 13
        $host = '' . $this->host;
46 13
        return (preg_match('#^[0-9a-f:]+$#', $host) ? '[' . $host . ']' : $host ); // IPv6
47
    }
48
49 8
    public function getPort(): ?int
50
    {
51 8
        return empty($this->port) ? null : intval($this->port);
52
    }
53
54 1
    public function getTimeout(): ?float
55
    {
56 1
        return empty($this->timeout) ? null : floatval($this->timeout);
57
    }
58
59 2
    public function getProtocolVersion(): int
60
    {
61 2
        return 0;
62
    }
63
64
    /**
65
     * Get one of available IP network packet wrappers
66
     * default behavior falls into TCP by PHP
67
     * @return string
68
     */
69 10
    public function getSchema(): string
70
    {
71 10
        return in_array($this->getSchemaType(), [
72 10
                Interfaces\ISchema::SCHEMA_FILE,
73 10
                Interfaces\ISchema::SCHEMA_PHP,
74 10
                Interfaces\ISchema::SCHEMA_TCP,
75 10
                Interfaces\ISchema::SCHEMA_UDP,
76 10
                Interfaces\ISchema::SCHEMA_SSL,
77 10
            ])
78 10
            ? ($this->getSchemaType() . '://')
79 10
            : ''
80 10
        ;
81
    }
82
}
83