Stream   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 50
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setContextOptions() 0 4 1
A remotePointer() 0 29 5
1
<?php
2
3
namespace kalanis\RemoteRequest\Sockets;
4
5
6
use kalanis\RemoteRequest\Interfaces\IConnectionParams;
7
use kalanis\RemoteRequest\RequestException;
8
9
10
/**
11
 * Class Stream
12
 * @package kalanis\RemoteRequest\Sockets
13
 * Network pointer to the remote server - method Stream
14
 * Because that little shit cannot set context options to Fsocket; namely devel certs
15
 */
16
class Stream extends ASocket
17
{
18
    /** @var array<string, string|array<string, string>> */
19
    protected array $contextOptions = [];
20
21
    /**
22
     * @param array<string, string|array<string, string>> $contextOptions
23
     * @return $this
24
     */
25 2
    public function setContextOptions(array $contextOptions = []): self
26
    {
27 2
        $this->contextOptions = $contextOptions;
28 2
        return $this;
29
    }
30
31
    /**
32
     * @param IConnectionParams $params
33
     * @throws RequestException
34
     * @return resource
35
     * @codeCoverageIgnore because accessing remote source
36
     */
37
    protected function remotePointer(IConnectionParams $params)
38
    {
39
        // Make the request to the server
40
        // If possible, securely post using HTTPS, your PHP server will need to be SSL enabled
41
42
        // example array of context options for skipping devel certs
43
//        $contextOptions = [
44
//            'ssl' => [
45
//                'verify_peer' => !DEVEL_ENVIRONMENT, // You could skip all of the trouble by changing this to false, but it's WAY uncool for security reasons. // kecy...
46
//                'cafile' => '/etc/ssl/certs/cacert.pem',
47
//                'CN_match' => 'example.com', // Change this to your certificates Common Name (or just comment this line out if not needed)
48
//                'ciphers' => 'HIGH:!SSLv2:!SSLv3',
49
//                'disable_compression' => true,
50
//            ],
51
//        ];
52
53
        $context = stream_context_create($this->contextOptions);
54
        $link = $params->getSchema() . $params->getHost() . (!empty($params->getPort()) ? ':' . $params->getPort() : '' );
55
        $timeout = is_null($params->getTimeout()) ? 10.0 : floatval($params->getTimeout()); // do NOT ask - php7 + phpstan
56
57
        if (!$filePointer = stream_socket_client($link, $errno, $errStr, $timeout, STREAM_CLIENT_CONNECT, $context)) {
58
            throw new RequestException($this->getRRLang()->rrSocketCannotConnect());
59
        }
60
61
        if (!is_null($params->getTimeout())) {
62
            stream_set_timeout($filePointer, intval($params->getTimeout()));
63
        }
64
65
        return $filePointer;
66
    }
67
}
68