These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * Copyright 2014 Brian Smith <[email protected]>. |
||
5 | * |
||
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
7 | * you may not use this file except in compliance with the License. |
||
8 | * You may obtain a copy of the License at |
||
9 | * |
||
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
11 | * |
||
12 | * Unless required by applicable law or agreed to in writing, software |
||
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
15 | * See the License for the specific language governing permissions and |
||
16 | * limitations under the License. |
||
17 | */ |
||
18 | |||
19 | namespace phparia\Api; |
||
20 | |||
21 | use GuzzleHttp\Exception\RequestException; |
||
22 | use phparia\Client\AriClientAware; |
||
23 | use phparia\Resources\AsteriskInfo; |
||
24 | use phparia\Resources\Variable; |
||
25 | use phparia\Exception\InvalidParameterException; |
||
26 | |||
27 | /** |
||
28 | * Asterisk API |
||
29 | * |
||
30 | * @author Brian Smith <[email protected]> |
||
31 | */ |
||
32 | class Asterisk extends AriClientAware |
||
33 | { |
||
34 | const INFO_BUILD = 'build'; |
||
35 | const INFO_SYSTEM = 'system'; |
||
36 | const INFO_CONFIG = 'config'; |
||
37 | const INFO_STATUS = 'status'; |
||
38 | |||
39 | /** |
||
40 | * Gets Asterisk system information. |
||
41 | * |
||
42 | * @param string $only Filter information returned. Allows comma separated values. Allowed values: build, system, config, status. |
||
43 | * @return AsteriskInfo |
||
44 | */ |
||
45 | 2 | public function getInfo($only = null) |
|
46 | { |
||
47 | 2 | if (empty($only)) { |
|
48 | 1 | $uri = 'asterisk/info'; |
|
49 | 1 | } else { |
|
50 | 1 | $uri = "asterisk/info?only=$only"; |
|
51 | } |
||
52 | 2 | $response = $this->client->getEndpoint()->get($uri); |
|
53 | |||
54 | 2 | return new AsteriskInfo(\GuzzleHttp\json_decode($response->getBody())); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get the value of a global variable. |
||
59 | * |
||
60 | * @param string $variable (required) The variable to get |
||
61 | * @return Variable |
||
62 | * @throws InvalidParameterException |
||
63 | */ |
||
64 | 2 | View Code Duplication | public function getVariable($variable) |
0 ignored issues
–
show
|
|||
65 | { |
||
66 | 2 | $uri = "asterisk/variable?variable=$variable"; |
|
67 | |||
68 | try { |
||
69 | 2 | $response = $this->client->getEndpoint()->get($uri); |
|
70 | 2 | } catch (RequestException $e) { |
|
71 | 1 | $this->processRequestException($e); |
|
72 | } |
||
73 | |||
74 | 1 | return new Variable(\GuzzleHttp\json_decode($response->getBody())); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Set the value of a global variable. |
||
79 | * |
||
80 | * @param string $variable (required) The variable to set |
||
81 | * @param string $value The value to set the variable to |
||
82 | * @throws InvalidParameterException |
||
83 | */ |
||
84 | 2 | public function setVariable($variable, $value = null) |
|
85 | { |
||
86 | 2 | $uri = 'asterisk/variable'; |
|
87 | |||
88 | try { |
||
89 | 2 | $this->client->getEndpoint()->post($uri, [ |
|
90 | 2 | 'form_params' => [ |
|
91 | 'variable' => $variable, |
||
92 | 2 | 'value' => $value |
|
93 | 2 | ] |
|
94 | 1 | ]); |
|
95 | } catch (RequestException $e) { |
||
96 | 1 | $this->processRequestException($e); |
|
97 | } |
||
98 | } |
||
99 | } |
||
100 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.