Events::getEvents()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 11
cp 0
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\Events\Event;
24
use phparia\Events\EventInterface;
25
use phparia\Exception\InvalidParameterException;
26
use phparia\Exception\NotFoundException;
27
use phparia\Exception\UnprocessableEntityException;
28
29
/**
30
 * Events API
31
 *
32
 * @author Brian Smith <[email protected]>
33
 */
34
class Events extends AriClientAware
35
{
36
    /**
37
     * WebSocket connection for events.
38
     *
39
     * @param string $app (required) Applications to subscribe to.  Allows comma separated values.
40
     * @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'.
41
     * @return EventInterface[]
42
     */
43
    public function getEvents($app, $subscribeAll = false)
44
    {
45
        $uri = 'events';
46
        $response = $this->client->getEndpoint()->get($uri, [
47
            'form_params' => [
48
                'app' => $app,
49
                'subscribeAll' => $subscribeAll,
50
            ]
51
        ]);
52
53
        $events = [];
54
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $event) {
55
            $events[] = new Event($this->client, $event);
56
        }
57
58
        return $events;
59
    }
60
61
    /**
62
     *
63
     * @param string $eventName Event name
64
     * @param string $application (required) The name of the application that will receive this event
65
     * @param string $source URI for event source (channel:{channelId}, bridge:{bridgeId}, endpoint:{tech}/{resource}, deviceState:{deviceName}  Allows comma separated values.
66
     * @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" }
67
     * }
68
     * @throws InvalidParameterException
69
     * @throws NotFoundException
70
     * @throws UnprocessableEntityException
71
     */
72 View Code Duplication
    public function createUserEvent($eventName, $application, $source, $variables = array())
73
    {
74
        $uri = "events/user/$eventName";
75
        try {
76
            $this->client->getEndpoint()->post($uri, [
77
                'form_params' => [
78
                    'application' => $application,
79
                    'source' => $source,
80
                    'variables' => array_map('strval', $variables),
81
                ]
82
            ]);
83
        } catch (RequestException $e) {
84
            $this->processRequestException($e);
85
        }
86
    }
87
}
88