Passed
Push — master ( 15770c...ea0752 )
by Petr
08:09
created

AParams::setTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    /** @var string */
19
    protected $host = '';
20
    /** @var int */
21
    protected $port = 1;
22
    /** @var int|float */
23
    protected $timeout = 30.0;
24
25
    abstract protected function getSchemaType(): string;
26
27 12
    public function setTarget(string $host = null, int $port = null, int $timeout = null): self
28
    {
29 12
        $this->host = $host ?? $this->host;
30 12
        $this->port = $port ?? $this->port;
31 12
        $this->timeout = $timeout ?? $this->timeout;
32 12
        return $this;
33
    }
34
35 1
    public function setRequest(Interfaces\ITarget $request): self
36
    {
37 1
        $this->host = $request->getHost();
38 1
        $this->port = $request->getPort() ?? $this->port;
39 1
        return $this;
40
    }
41
42
    /**
43
     * Generate correct hostname
44
     * This method updates IPv6 address into form that is usable by sockets
45
     */
46 13
    public function getHost(): string
47
    {
48 13
        $host = '' . $this->host;
49 13
        return (preg_match('#^[0-9a-f:]+$#', $host) ? '[' . $host . ']' : $host ); // IPv6
50
    }
51
52 8
    public function getPort(): ?int
53
    {
54 8
        return empty($this->port) ? null : intval($this->port);
55
    }
56
57 1
    public function getTimeout(): ?float
58
    {
59 1
        return empty($this->timeout) ? null : floatval($this->timeout);
60
    }
61
62 2
    public function getProtocolVersion(): int
63
    {
64 2
        return 0;
65
    }
66
67
    /**
68
     * Get one of available IP network packet wrappers
69
     * default behavior falls into TCP by PHP
70
     * @return string
71
     */
72 10
    public function getSchema(): string
73
    {
74 10
        return in_array($this->getSchemaType(), [
75
                Interfaces\ISchema::SCHEMA_FILE,
76
                Interfaces\ISchema::SCHEMA_PHP,
77
                Interfaces\ISchema::SCHEMA_TCP,
78
                Interfaces\ISchema::SCHEMA_UDP,
79
                Interfaces\ISchema::SCHEMA_SSL,
80
            ])
81 10
            ? ($this->getSchemaType() . '://')
82 10
            : ''
83
        ;
84
    }
85
}
86