GetTranslations::processResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the badams\MicrosoftTranslator library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/badams/microsoft-translator
7
 * @package badams/microsoft-translator
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace badams\MicrosoftTranslator\Methods;
14
15
use badams\MicrosoftTranslator\Exceptions\ArgumentException;
16
use badams\MicrosoftTranslator\Exceptions\UnsupportedLanguageException;
17
use badams\MicrosoftTranslator\Language;
18
use badams\MicrosoftTranslator\Responses\GetTranslationsResponse;
19
use badams\MicrosoftTranslator\TranslateOptions;
20
21
/**
22
 * Class GetTranslations
23
 * @package badams\MicrosoftTranslator\Methods
24
 * @link https://msdn.microsoft.com/en-us/library/ff512417.aspx
25
 */
26
class GetTranslations implements \badams\MicrosoftTranslator\ApiMethodInterface
27
{
28
    /**
29
     * @const Maximum allowable length of text
30
     */
31
    const TEXT_MAX_LENGTH = 10000;
32
33
    /**
34
     * @var string
35
     */
36
    protected $text;
37
38
    /**
39
     * @var Language
40
     */
41
    protected $to;
42
43
    /**
44
     * @var Language|null
45
     */
46
    protected $from;
47
48
    /**
49
     * @var int
50
     */
51
    protected $maxTranslations;
52
53
    /**
54
     * @var TranslateOptions
55
     */
56
    protected $options;
57
58
    /**
59
     * GetTranslations constructor.
60
     * @param $text
61
     * @param $to
62
     * @param $from
63
     * @param int $maxTranslations
64
     * @param TranslateOptions|null $options
65
     * @throws ArgumentException
66
     * @throws UnsupportedLanguageException
67
     */
68 12
    public function __construct($text, $to, $from, $maxTranslations = 5, TranslateOptions $options = null)
69
    {
70 12
        $this->text = $text;
71 12
        $this->to = new Language($to);
72 9
        $this->from = new Language($from);
73 9
        $this->maxTranslations = $maxTranslations;
74 9
        $this->options = $options ? $options : new TranslateOptions();
75
76 9
        if (strlen($text) > GetTranslations::TEXT_MAX_LENGTH) {
77 3
            throw new ArgumentException(
78 3
                sprintf('The length of the text must not exceed %s characters.', GetTranslations::TEXT_MAX_LENGTH)
79 3
            );
80
        }
81 6
    }
82
83
    /**
84
     * @return string
85
     */
86 6
    public function getRequestMethod()
87
    {
88 6
        return 'POST';
89
    }
90
91
    /**
92
     * @return array
93
     */
94 6
    public function getRequestOptions()
95
    {
96
        return [
97 6
            'body' => (string)$this->options,
98
            'query' => [
99 6
                'text' => $this->text,
100 6
                'to' => (string)$this->to,
101 6
                'from' => (string)$this->from,
102 6
                'maxTranslations' => $this->maxTranslations,
103
            ]
104 6
        ];
105
    }
106
107
    /**
108
     * @param \GuzzleHttp\Message\ResponseInterface $response
109
     * @return GetTranslationsResponse
110
     */
111 6
    public function processResponse(\GuzzleHttp\Message\ResponseInterface $response)
112
    {
113 6
        $xml = simplexml_load_string($response->getBody()->getContents());
114
115 6
        return GetTranslationsResponse::fromXml($xml);
116
    }
117
}
118