Completed
Push — master ( 1ca3b7...aef33f )
by Gallice
05:16
created

Api   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 112
ccs 30
cts 32
cp 0.9375
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConverseNextStep() 0 11 1
A getIntentByText() 0 14 2
B getIntentBySpeech() 0 15 6
A getMessage() 0 6 1
A getClient() 0 4 1
A decodeResponse() 0 4 1
1
<?php
2
3
namespace Tgallice\Wit;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Tgallice\Wit\Model\Context;
7
8
class Api
9
{
10
    /**
11
     * @var Client client
12
     */
13
    private $client;
14
15
    /**
16
     * @param Client $client
17
     */
18 8
    public function __construct(Client $client)
19
    {
20 8
        $this->client = $client;
21 8
    }
22
23
    /**
24
     * @param string $sessionId
25
     * @param string $text
26
     * @param Context|null $context
27
     *
28
     * @return array|null
29
     */
30 1
    public function getConverseNextStep($sessionId, $text, Context $context = null)
31
    {
32
        $query = [
33 1
            'session_id' => $sessionId,
34 1
            'q' => $text,
35 1
        ];
36
37 1
        $response = $this->client->send('POST', '/converse', $context, $query);
38
39 1
        return $this->decodeResponse($response);
0 ignored issues
show
Deprecated Code introduced by
The method Tgallice\Wit\Api::decodeResponse() has been deprecated with message: This method is deprecated as of 0.1 and will be removed in 1.0. You should use the ResponseHandler::decodeResponse() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
40
    }
41
42
    /**
43
     * @param string $text
44
     * @param Context|null $context
45
     * @param array $extraParams
46
     *
47
     * @return array|null
48
     */
49 2
    public function getIntentByText($text, Context $context = null, array $extraParams = [])
50
    {
51 2
        $query = array_merge($extraParams, [
52 2
            'q' => $text,
53 2
        ]);
54
55 2
        if (null !== $context) {
56 2
            $query['context'] = json_encode($context);
57 2
        }
58
59 2
        $response = $this->client->get('/message', $query);
60
61 2
        return $this->decodeResponse($response);
0 ignored issues
show
Deprecated Code introduced by
The method Tgallice\Wit\Api::decodeResponse() has been deprecated with message: This method is deprecated as of 0.1 and will be removed in 1.0. You should use the ResponseHandler::decodeResponse() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
    }
63
64
    /**
65
     * @param string|resource $file
66
     * @param array|null $context
67
     * @param array $queryParams
68
     *
69
     * @return array|null
70
     */
71 3
    public function getIntentBySpeech($file, Context $context = null, array $queryParams = [])
72
    {
73 3
        if (!$file || (!is_resource($file) && !is_readable($file))) {
74 1
            throw new \InvalidArgumentException('$file argument must be a readable file path or a valid resource');
75
        }
76
77 2
        if (null !== $context) {
78 2
            $queryParams['context'] = json_encode($context);
79 2
        }
80
81 2
        $file = is_resource($file) ? $file : fopen($file, 'r');
82 2
        $response = $this->client->send('POST', '/speech', $file, $queryParams);
83
84 2
        return $this->decodeResponse($response);
0 ignored issues
show
Deprecated Code introduced by
The method Tgallice\Wit\Api::decodeResponse() has been deprecated with message: This method is deprecated as of 0.1 and will be removed in 1.0. You should use the ResponseHandler::decodeResponse() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
85
    }
86
87
    /**
88
     * @param string $messageId
89
     *
90
     * @return array|null
91
     */
92 1
    public function getMessage($messageId)
93
    {
94 1
        $response = $this->client->get(sprintf('/messages/%s', $messageId));
95
96 1
        return $this->decodeResponse($response);
0 ignored issues
show
Deprecated Code introduced by
The method Tgallice\Wit\Api::decodeResponse() has been deprecated with message: This method is deprecated as of 0.1 and will be removed in 1.0. You should use the ResponseHandler::decodeResponse() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
97
    }
98
99
    /**
100
     * @return Client
101
     */
102
    public function getClient()
103
    {
104
        return $this->client;
105
    }
106
107
    /**
108
     * @deprecated This method is deprecated as of 0.1 and will be removed in 1.0.
109
     *             You should use the ResponseHandler::decodeResponse() instead
110
     *
111
     * @param ResponseInterface $response
112
     *
113
     * @return array|null
114
     */
115
    protected function decodeResponse(ResponseInterface $response)
116
    {
117
        return json_decode((string) $response->getBody(), true);
118
    }
119
}
120