Completed
Push — develop ( 9b1d71...66130e )
by Adam
03:07 queued 01:21
created

ToneChat::validateUtterances()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
namespace IBM\Watson\ToneAnalyzer\Api;
4
5
use IBM\Watson\Common\Api\AbstractApi;
6
use IBM\Watson\ToneAnalyzer\Model\Utterance;
7
use IBM\Watson\ToneAnalyzer\Model\UtteranceAnalyses;
8
9
/**
10
 * Analyze customer engagement tone.
11
 *
12
 * Use the customer engagement endpoint to analyze the tone of customer service and customer support conversations.
13
 * For each utterance of a conversation, the method reports the most prevalent subset of the following seven tones:
14
 * sad, frustrated, satisfied, excited, polite, impolite, and sympathetic.
15
 *
16
 * If you submit more than 50 utterances, the service returns a warning for the overall content and analyzes only the
17
 * first 50 utterances. If you submit a single utterance that contains more than 500 characters, the service returns
18
 * an error for that utterance and does not analyze the utterance. The request fails if all utterances have more than
19
 * 500 characters.
20
 *
21
 * Per the JSON specification, the default character encoding for JSON content is effectively always UTF-8.
22
 */
23
class ToneChat extends AbstractApi
24
{
25
    /**
26
     * @var string
27
     */
28
    const ENDPOINT = '/tone_chat';
29
30
    /**
31
     * @return \IBM\Watson\Common\Api\AbstractApi|void
32
     */
33
    protected function setAllowedParams()
34
    {
35
        $this->allowedParams = [
36
            'utterances',
37
            'content_language',
38
            'accept_language'
39
        ];
40
    }
41
42
    /**
43
     * @param array $utterances
44
     * @param array $params
45
     *
46
     * @return \IBM\Watson\ToneAnalyzer\Model\UtteranceAnalysis
47
     *
48
     * @throws \Http\Client\Exception
49
     * @throws \IBM\Watson\Common\Exception\Api\BadRequestException
50
     * @throws \IBM\Watson\Common\Exception\Domain\InsufficientPrivilegesException
51
     * @throws \IBM\Watson\Common\Exception\Domain\NotFoundException
52
     * @throws \IBM\Watson\Common\Exception\Domain\UnknownErrorException
53
     */
54
    public function analyze(array $utterances, array $params = [])
55
    {
56
        $utterances = $this->validateUtterances($utterances);
57
58
        $params['utterances'] = $utterances;
59
60
        $response = $this->postRaw(self::ENDPOINT, json_encode($params), [
61
            'Content-Type' => 'application/json'
62
        ]);
63
64
        if ($response->getStatusCode() !== 200) {
65
            $this->handleErrors($response);
66
        }
67
68
        return $this->hydrator->hydrate($response, UtteranceAnalyses::class);
69
    }
70
71
    /**
72
     * @param array $utterances
73
     *
74
     * @return array
75
     */
76
    private function validateUtterances(array $utterances)
77
    {
78
        $return = [];
79
80
        foreach ($utterances as $utterance) {
81
            $return[] = [
82
                Utterance::KEY_TEXT => $utterance instanceof Utterance ? $utterance->getText()
83
                    : $utterance[Utterance::KEY_TEXT],
84
                Utterance::KEY_USER => $utterance instanceof Utterance ? $utterance->getUser()
85
                    : $utterance[Utterance::KEY_USER]
86
            ];
87
        }
88
89
        return $return;
90
    }
91
}
92