Failed Conditions
Push — master ( 01d6ae...9ca6d9 )
by Philippe
534:14 queued 469:10
created

LanguageToolApiClient::getSupportedLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Spellchecker\LanguageTool;
6
7
class LanguageToolApiClient
8
{
9
    /**
10
     * @var string
11
     */
12
    private $baseUrl;
13
14 2
    public function __construct(string $baseUrl)
15
    {
16 2
        $this->baseUrl = $baseUrl;
17 2
    }
18
19 1
    public function spellCheck(string $text, array $languages, array $options): array
20
    {
21 1
        $options['text'] = $text;
22 1
        $options['language'] = array_shift($languages);
23 1
        $options['altLanguages'] = implode(',', $languages);
24
25 1
        return $this->requestAPI(
26 1
            '/v2/check',
27 1
            'POST',
28 1
            'Content-type: application/x-www-form-urlencoded; Accept: application/json',
29 1
            $options
30
        );
31
    }
32
33 1
    public function getSupportedLanguages(): array
34
    {
35 1
        return array_column(
36 1
            $this->requestAPI(
37 1
                '/v2/languages',
38 1
                'GET',
39 1
                'Accept: application/json'
40
            ),
41 1
            'longCode'
42
        );
43
    }
44
45
    /**
46
     * @throws \RuntimeException
47
     */
48 2
    public function requestAPI(string $endpoint, string $method, string $header, array $queryParams = []): array
49
    {
50
        $httpData = [
51 2
            'method' => $method,
52 2
            'header' => $header,
53
        ];
54
55 2
        if (!empty($queryParams)) {
56 1
            $httpData['content'] = http_build_query($queryParams);
57
        }
58
59 2
        $content = \Safe\file_get_contents($this->baseUrl . $endpoint, false, stream_context_create(['http' => $httpData]));
60
61 2
        return \Safe\json_decode($content, true);
62 2
    }
63
}
64