Completed
Push — develop ( 68a985...a1f82e )
by Adam
10s
created

Tone::analyze()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.0045

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 19
cts 20
cp 0.95
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 32
nop 2
crap 6.0045
1
<?php
2
3
namespace IBM\Watson\ToneAnalyzer\Api;
4
5
use IBM\Watson\Common\Api\AbstractApi;
6
use IBM\Watson\ToneAnalyzer\Model\ToneAnalysis;
7
8
class Tone extends AbstractApi
9
{
10
    /**
11
     * Analyze tone
12
     *
13
     * @param            $text
14
     * @param array|null $params
15
     *
16
     * @return mixed|\Psr\Http\Message\ResponseInterface
17
     *
18
     * @throws \IBM\Watson\Common\Exception\UnknownErrorException
19
     * @throws \IBM\Watson\Common\Exception\NotFoundException
20
     * @throws \IBM\Watson\Common\Exception\InsufficientPrivilegesException
21
     * @throws \Http\Client\Exception
22
     * @throws \IBM\Watson\ToneAnalyzer\Exception
23
     * @throws \Exception
24
     */
25 15
    public function analyze($text, array $params = null)
26
    {
27 15
        $headers = [];
28
29 15
        $params['text'] = $text;
30
31 15
        if (!isset($params['version'])) {
32 15
            $params['version'] = \date('Y-m-d');
33 10
        }
34
35 15
        if (!isset($params['sentences'])) {
36 15
            $params['sentences'] = true;
37 10
        }
38
39 15
        if (isset($params['content_language'])) {
40 6
            $headers['Content-Language'] = $params['content_language'];
41 4
        }
42
43 15
        if (isset($params['accept_language'])) {
44 6
            $headers['Accept-Language'] = $params['accept_language'];
45 4
        }
46
47 15
        $response = $this->get('/api/v3/tone', $params, $headers);
48
49 15
        if ($response->getStatusCode() !== 200) {
50 9
            $this->handleErrors($response);
51
        }
52
53 6
        return $this->hydrator->hydrate($response, ToneAnalysis::class);
54
    }
55
}
56