GetLanguageNames::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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\Language;
16
17
/**
18
 * Class GetLanguageNames
19
 * @package badams\MicrosoftTranslator\Methods
20
 */
21
class GetLanguageNames implements \badams\MicrosoftTranslator\ApiMethodInterface
22
{
23
    protected $locale;
24
25
    protected $languageCodes;
26
27
28 9
    public function __construct($locale, $languageCodes)
29
    {
30 9
        $this->locale = new Language($locale);
31 6
        $this->languageCodes = $languageCodes;
32 6
    }
33
34
    /**
35
     * @return string
36
     */
37 6
    public function getRequestMethod()
38
    {
39 6
        return 'POST';
40
    }
41
42
    /**
43
     * @return array
44
     */
45 6
    public function getRequestOptions()
46
    {
47
        return [
48 6
            'query' => ['locale' => $this->locale],
49 6
            'body' => $this->createBody(),
50 6
        ];
51
    }
52
53
    /**
54
     * @param \GuzzleHttp\Message\ResponseInterface $response
55
     * @return Language[]
56
     */
57 6
    public function processResponse(\GuzzleHttp\Message\ResponseInterface $response)
58
    {
59 6
        $xml = simplexml_load_string($response->getBody()->getContents());
60 6
        $languages = [];
61
62 6
        foreach ($xml->string as $language) {
63 6
            $language = (string)$language;
64 6
            if (!empty($language)) {
65 3
                $languages[] = $language;
66 3
            }
67 6
        }
68
69 6
        return $languages;
70
    }
71
72
    /**
73
     * @return string
74
     */
75 6
    private function createBody()
76
    {
77 6
        $xml = new \DOMDocument();
78 6
        $array = $xml->createElementNS('http://schemas.microsoft.com/2003/10/Serialization/Arrays', 'ArrayOfstring');
79
80 6
        foreach ($this->languageCodes as $code) {
81 6
            $array->appendChild($xml->createElement('string', $code));
82 6
        }
83
84 6
        $xml->appendChild($array);
85 6
        return $xml->saveXML();
86
    }
87
}
88