Completed
Push — master ( a5e5f2...dcb46f )
by Brian
02:47
created

Applications::getApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.1481
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_Conflict;
23
use Pest_InvalidRecord;
24
use Pest_NotFound;
25
use phparia\Client\AriClientAware;
26
use phparia\Exception\ConflictException;
27
use phparia\Exception\InvalidParameterException;
28
use phparia\Exception\NotFoundException;
29
use phparia\Exception\UnprocessableEntityException;
30
use phparia\Resources\Application;
31
32
/**
33
 * Applications API
34
 *
35
 * @author Brian Smith <[email protected]>
36
 */
37
class Applications extends AriClientAware
38
{
39
40
    /**
41
     * List all getApplications.
42
     *
43
     * @return Application[]
44
     */
45 1
    public function getApplications()
46
    {
47 1
        $uri = '/applications';
48 1
        $response = $this->client->getEndpoint()->get($uri);
49
50
        $applications = [];
51
        foreach ((array)$response as $application) {
52
            $applications[] = new Application($this->client, $application);
53
        }
54
55
        return $applications;
56
    }
57
58
    /**
59
     * Get details of an application.
60
     *
61
     * @param $applicationName
62
     * @return Application
63
     * @throws NotFoundException
64
     */
65 2
    public function getApplication($applicationName)
66
    {
67 2
        $uri = "/applications/$applicationName";
68
        try {
69 2
            $response = $this->client->getEndpoint()->get($uri);
70 2
        } catch (Pest_NotFound $e) { // Application does not exist.
71
            throw new NotFoundException($e);
72
        }
73
74
        return new Application($this->client, $response);
75
    }
76
77
    /**
78
     * Subscribe an application to a event source. Returns the state of the application after the subscriptions have changed
79
     *
80
     * @param string $applicationName Application's name
81
     * @param string $eventSource (required) URI for event source (channel:{channelId}, bridge:{bridgeId}, endpoint:{tech}[/{resource}], deviceState:{deviceName}.  Allows comma separated values.
82
     * @return Application
83
     * @throws InvalidParameterException
84
     * @throws NotFoundException
85
     * @throws UnprocessableEntityException
86
     */
87 3
    public function subscribe($applicationName, $eventSource)
88
    {
89 3
        $uri = "/applications/$applicationName/subscription";
90
        try {
91 3
            $response = $this->client->getEndpoint()->post($uri, array(
92 3
                'eventSource' => $eventSource,
93 3
            ));
94 3
        } catch (Pest_BadRequest $e) { // Missing parameter.
95
            throw new InvalidParameterException($e);
96 3
        } catch (Pest_NotFound $e) { // Application does not exist.
97
            throw new NotFoundException($e);
98 3
        } catch (Pest_InvalidRecord $e) { // Event source does not exist.
99
            throw new UnprocessableEntityException($e);
100
        }
101
102
        return new Application($this->client, $response);
103
    }
104
105
    /**
106
     * Unsubscribe an application from an event source. Returns the state of the application after the subscriptions have changed
107
     *
108
     * @param string $applicationName Application's name
109
     * @param string $eventSource (required) URI for event source (channel:{channelId}, bridge:{bridgeId}, endpoint:{tech}[/{resource}], deviceState:{deviceName}  Allows comma separated values.
110
     * @return Application
111
     * @throws ConflictException
112
     * @throws InvalidParameterException
113
     * @throws NotFoundException
114
     * @throws UnprocessableEntityException
115
     */
116 3
    public function unsubscribe($applicationName, $eventSource)
117
    {
118 3
        $uri = "/applications/$applicationName/subscription?eventSource=".$this->client->getEndpoint()->jsonEncode($eventSource);
119
        try {
120 3
            $response = $this->client->getEndpoint()->delete($uri);
121 3
        } catch (Pest_BadRequest $e) { // Missing parameter; event source scheme not recognized.
122
            throw new InvalidParameterException($e);
123 3
        } catch (Pest_NotFound $e) { // Application does not exist.
124
            throw new NotFoundException($e);
125 3
        } catch (Pest_Conflict $e) { // Application not subscribed to event source.
126
            throw new ConflictException($e);
127 3
        } catch (Pest_InvalidRecord $e) { // Event source does not exist.
128
            throw new UnprocessableEntityException($e);
129
        }
130
131
        return new Application($this->client, $response);
132
    }
133
134
}
135