Completed
Push — develop ( a1f82e...6f4353 )
by Adam
15:09 queued 12s
created

Tone::analyze()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 64
nop 2
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
    public function analyze($text, array $params = null)
26
    {
27
        $headers = [];
28
29
        $params['text'] = $text;
30
31
        if (!isset($params['version'])) {
32
            $params['version'] = \date('Y-m-d');
33
        }
34
35
        if (!isset($params['sentences'])) {
36
            $params['sentences'] = true;
37
        }
38
39
        if (isset($params['content_language'])) {
40
            $headers['Content-Language'] = $params['content_language'];
41
        }
42
43
        if (isset($params['accept_language'])) {
44
            $headers['Accept-Language'] = $params['accept_language'];
45
        }
46
47
        if (isset($params['learning_opt_out'])) {
48
            $headers['X-Watson-Learning-Opt-Out'] = true;
49
        }
50
51
        $response = $this->get('/api/v3/tone', $params, $headers);
52
53
        if ($response->getStatusCode() !== 200) {
54
            $this->handleErrors($response);
55
        }
56
57
        return $this->hydrator->hydrate($response, ToneAnalysis::class);
58
    }
59
}
60