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 Pest_BadRequest; |
22
|
|
|
use Pest_InvalidRecord; |
23
|
|
|
use Pest_NotFound; |
24
|
|
|
use phparia\Client\AriClientAware; |
25
|
|
|
use phparia\Events\Event; |
26
|
|
|
use phparia\Events\EventInterface; |
27
|
|
|
use phparia\Exception\InvalidParameterException; |
28
|
|
|
use phparia\Exception\NotFoundException; |
29
|
|
|
use phparia\Exception\UnprocessableEntityException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Events API |
33
|
|
|
* |
34
|
|
|
* @author Brian Smith <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class Events extends AriClientAware |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* WebSocket connection for events. |
41
|
|
|
* |
42
|
|
|
* @param string $app (required) Applications to subscribe to. Allows comma separated values. |
43
|
|
|
* @param boolean $subscribeAll Subscribe to all Asterisk events. If provided, the applications listed will be subscribed to all events, effectively disabling the application specific subscriptions. Default is 'false'. |
44
|
|
|
* @return EventInterface[] |
45
|
|
|
*/ |
46
|
1 |
|
public function getEvents($app, $subscribeAll = false) |
47
|
|
|
{ |
48
|
1 |
|
$uri = '/events'; |
49
|
1 |
|
$response = $this->client->getEndpoint()->get($uri, array( |
50
|
1 |
|
'app' => $app, |
51
|
1 |
|
'subscribeAll' => $subscribeAll, |
52
|
1 |
|
)); |
53
|
|
|
|
54
|
|
|
$events = []; |
55
|
|
|
foreach ((array)$response as $event) { |
56
|
|
|
$events[] = new Event($this->client, $event); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $events; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @param string $eventName Event name |
65
|
|
|
* @param string $application (required) The name of the application that will receive this event |
66
|
|
|
* @param string $source URI for event source (channel:{channelId}, bridge:{bridgeId}, endpoint:{tech}/{resource}, deviceState:{deviceName} Allows comma separated values. |
67
|
|
|
* @param array $variables The "variables" key in the body object holds custom key/value pairs to add to the user event. Ex. { "variables": { "key": "value" } |
68
|
|
|
* } |
69
|
|
|
* @throws InvalidParameterException |
70
|
|
|
* @throws NotFoundException |
71
|
|
|
* @throws UnprocessableEntityException |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function createUserEvent($eventName, $application, $source, $variables = array()) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$uri = "/events/user/$eventName"; |
76
|
|
|
try { |
77
|
|
|
$this->client->getEndpoint()->post($uri, array( |
78
|
|
|
'application' => $application, |
79
|
|
|
'source' => $source, |
80
|
|
|
'variables' => array_map('strval', $variables), |
81
|
|
|
)); |
82
|
|
|
} catch (Pest_BadRequest $e) { // Invalid parameters |
83
|
|
|
throw new InvalidParameterException($e); |
84
|
|
|
} catch (Pest_NotFound $e) { // Channel not found |
85
|
|
|
throw new NotFoundException($e); |
86
|
|
|
} catch (Pest_InvalidRecord $e) { // Channel not in Stasis application |
87
|
|
|
throw new UnprocessableEntityException($e); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.