Passed
Push — master ( 39a14a...98f505 )
by Scrutinizer
01:35
created

ConfigProtectedMethods::setUrlHost()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 21
rs 9.8333
cc 4
nc 7
nop 1
1
<?php
2
3
/**
4
 * Define protected methods and properties.
5
 *
6
 * Note that the only public method allowed is "__construct()". Other public
7
 * methods and properties must be defined in "Config" child class.
8
 */
9
10
declare(strict_types=1);
11
12
namespace KEINOS\MSTDN_TOOLS;
13
14
use Symfony\Component\HttpClient\HttpClient as HttpClient;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpClient\HttpClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class ConfigProtectedMethods extends ConfigStaticMethods
17
{
18
    /** @var string */
19
    protected $url_host;
20
    /** @var string */
21
    protected $url_api_instance;
22
    /** @var array<mixed,mixed> */
23
    protected $info_server;
24
25
    public function __construct(string $url_host)
26
    {
27
        $this->setUrlHost($url_host);
28
        $this->setServerInfo();
29
    }
30
31
    /**
32
     * @return array<mixed,mixed>
33
     */
34
    protected function getContentsAsArray(string $url_api): array
35
    {
36
        try {
37
            /** @phan-suppress-next-line PhanUndeclaredClassMethod */
38
            $client_http = HttpClient::create();
39
            $response = $client_http->request('GET', $url_api);
40
            // Get contents as array
41
            return $response->toArray();
42
        } catch (\Exception $e) {
43
            $msg = 'Error while setting server info.' . PHP_EOL
44
                 . '- Error details: ' . $e->getMessage() . PHP_EOL;
45
            throw new \Exception($msg);
46
        }
47
    }
48
49
    protected function setServerInfo(): void
50
    {
51
        $url_api = rtrim($this->url_host, '/') . self::ENDPOINT_API_INFO_INSTANCE;
52
        $info    = $this->getContentsAsArray($url_api);
53
        // Set property
54
        $this->url_api_instance = $url_api;
55
        $this->info_server = $info;
56
    }
57
58
    /**
59
     * @param  string $url_host  The URL of Mastodon server/instance.
60
     * @return void
61
     * @throws \Exception        If the server isn't available.
62
     */
63
    protected function setUrlHost(string $url_host): void
64
    {
65
        try {
66
            if (self::isUrlProtocolHttp($url_host)) {
67
                $msg = 'Not in "https:" protocol. We do not allow "http:" protocol.';
68
                throw new \Exception($msg);
69
            }
70
71
            /** @phan-suppress-next-line PhanUndeclaredClassMethod */
72
            $client_http = HttpClient::create();
73
74
            $response = $client_http->request('HEAD', $url_host);
75
            if (200 !== $response->getStatusCode()) {
76
                throw new \Exception('The server did not return 200 status.');
77
            }
78
            // Set property
79
            $this->url_host = $url_host;
80
        } catch (\Exception $e) {
81
            $msg = 'Error while setting server host URL.' . PHP_EOL
82
                 . '- Error details: ' . $e->getMessage() . PHP_EOL;
83
            throw new \Exception($msg);
84
        }
85
    }
86
}
87