Completed
Push — main ( 9e059c...65e74f )
by Dante
20s queued 14s
created

TranslateClient   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 15 1
A apiCall() 0 15 1
A __construct() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2023 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace BEdita\I18n\Microsoft\Core;
16
17
use Cake\Utility\Hash;
18
19
class TranslateClient
20
{
21
    /**
22
     * API endpoint.
23
     *
24
     * @var string
25
     */
26
    protected string $endpoint = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0';
27
28
    /**
29
     * The headers.
30
     *
31
     * @var array
32
     */
33
    protected array $headers = [];
34
35
    /**
36
     * The options.
37
     *
38
     * @var array
39
     */
40
    protected array $options = [];
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param array $options The options
46
     */
47
    public function __construct(array $options)
48
    {
49
        $this->options = $options;
50
        $this->headers = [
51
            'Content-Type' => 'application/json',
52
            'Ocp-Apim-Subscription-Key' => (string)Hash::get($this->options, 'auth_key'),
53
            'Ocp-Apim-Subscription-Region' => (string)Hash::get($this->options, 'location'),
54
        ];
55
    }
56
57
    /**
58
     * Translate a text from a language to another using Microsoft translator API.
59
     *
60
     * @param string $text The text to translate
61
     * @param string $from The source language
62
     * @param string $to The target language
63
     * @return string The translated text
64
     */
65
    public function translate(string $text, string $from, string $to): string
66
    {
67
        $headers = $this->headers;
68
        $body = json_encode([['Text' => $text]]);
69
        $headers['Content-Length'] = strlen($body);
70
        $headers = array_map(
71
            function ($key, $val) {
72
                return sprintf('%s: %s', $key, $val);
73
            },
74
            array_keys($headers),
75
            array_values($headers)
76
        );
77
        $url = sprintf('%s&from=%s&to=%s', $this->endpoint, $from, $to);
78
79
        return $this->apiCall($url, $body, $headers);
80
    }
81
82
    /**
83
     * Perform api call to obtain translation
84
     *
85
     * @param string $url The url to call
86
     * @param string $body The json body to pass to api call
87
     * @param array $headers The headers
88
     * @return string The translation, if any
89
     * @codeCoverageIgnore
90
     */
91
    public function apiCall(string $url, string $body, array $headers): string
92
    {
93
        $ch = curl_init($url);
94
        curl_setopt($ch, CURLOPT_POST, true);
95
        curl_setopt($ch, CURLOPT_URL, $url);
96
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
97
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
98
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
99
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
100
        $result = curl_exec($ch);
101
        curl_close($ch);
102
103
        return (string)Hash::get(
104
            json_decode($result, true),
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
            json_decode(/** @scrutinizer ignore-type */ $result, true),
Loading history...
105
            '0.translations.0.text'
106
        );
107
    }
108
}
109