Endpoints   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 56.91 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 6.67%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 70
loc 123
ccs 3
cts 45
cp 0.0667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEndpoints() 12 12 2
A sendMessage() 16 16 2
A getEndpointsByTech() 16 16 3
A getEndpointByTechAndResource() 11 11 2
A sendMessageToEndpointAndTechAndResource() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Exception\InvalidParameterException;
24
use phparia\Exception\NotFoundException;
25
use phparia\Resources\Endpoint;
26
27
/**
28
 * Endpoints API
29
 *
30
 * @author Brian Smith <[email protected]>
31
 */
32
class Endpoints extends AriClientAware
33
{
34
    const AST_ENDPOINT_UNKNOWN = 'unknown';
35
    const AST_ENDPOINT_OFFLINE = 'offline';
36
    const AST_ENDPOINT_ONLINE = 'online';
37
38
    /**
39
     * List all endpoints.
40
     *
41
     * @return Endpoint
42
     */
43 1 View Code Duplication
    public function getEndpoints()
44
    {
45 1
        $uri = 'endpoints';
46 1
        $response = $this->client->getEndpoint()->get($uri);
47
48
        $endpoints = [];
49
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $endpoint) {
50
            $endpoints[] = new Endpoint($this->client, $endpoint);
51
        }
52
53
        return $endpoints;
54
    }
55
56
    /**
57
     * Send a message to some technology URI or endpoint.
58
     *
59
     * @param string $to (required) The endpoint resource or technology specific URI to send the message to. Valid resources are sip, pjsip, and xmpp.
60
     * @param string $from (required) The endpoint resource or technology specific identity to send this message from. Valid resources are sip, pjsip, and xmpp.
61
     * @param string $body The body of the message
62
     * @param array $variables
63
     * @throws NotFoundException
64
     */
65 View Code Duplication
    public function sendMessage($to, $from, $body, $variables = array())
66
    {
67
        $uri = 'endpoints/sendMessage';
68
        try {
69
            $this->client->getEndpoint()->put($uri, [
70
                'form_params' => [
71
                    'to' => $to,
72
                    'from' => $from,
73
                    'body' => $body,
74
                    'variables' => array_map('strval', $variables),
75
                ]
76
            ]);
77
        } catch (RequestException $e) {
78
            $this->processRequestException($e);
79
        }
80
    }
81
82
    /**
83
     * List available endoints for a given endpoint technology.
84
     *
85
     * @param string $tech Technology of the endpoints (sip,iax2,...)
86
     * @return \phparia\Resources\Endpoint[]
87
     * @throws NotFoundException
88
     */
89 View Code Duplication
    public function getEndpointsByTech($tech)
90
    {
91
        $uri = "endpoints/$tech";
92
        try {
93
            $response = $this->client->getEndpoint()->get($uri);
94
        } catch (RequestException $e) {
95
            $this->processRequestException($e);
96
        }
97
98
        $endpoints = [];
99
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $endpoint) {
100
            $endpoints[] = new Endpoint($this->client, $endpoint);
101
        }
102
103
        return $endpoints;
104
    }
105
106
    /**
107
     * Details for an endpoint.
108
     *
109
     * @param string $tech Technology of the endpoint
110
     * @param string $resource ID of the endpoint
111
     * @return Endpoint
112
     * @throws InvalidParameterException
113
     * @throws NotFoundException
114
     */
115 View Code Duplication
    public function getEndpointByTechAndResource($tech, $resource)
116
    {
117
        $uri = "endpoints/$tech/$resource";
118
        try {
119
            $response = $this->client->getEndpoint()->get($uri);
120
        } catch (RequestException $e) {
121
            $this->processRequestException($e);
122
        }
123
124
        return new Endpoint($this->client, \GuzzleHttp\json_decode($response->getBody()));
125
    }
126
127
    /**
128
     * Send a message to some endpoint in a technology.
129
     *
130
     * @param string $tech
131
     * @param $resource
132
     * @param string $from (required) The endpoint resource or technology specific identity to send this message from. Valid resources are sip, pjsip, and xmpp.
133
     * @param string $body The body of the message
134
     * @param array $variables
135
     * @throws InvalidParameterException
136
     * @throws NotFoundException
137
     * @internal param $string @resource
138
     */
139 View Code Duplication
    public function sendMessageToEndpointAndTechAndResource($tech, $resource, $from, $body, $variables = array())
140
    {
141
        $uri = "endpoints/$tech/$resource/sendMessage";
142
        try {
143
            $this->client->getEndpoint()->put($uri, [
144
                'form_params' => [
145
                    'from' => $from,
146
                    'body' => $body,
147
                    'variables' => array_map('strval', $variables),
148
                ]
149
            ]);
150
        } catch (RequestException $e) {
151
            $this->processRequestException($e);
152
        }
153
    }
154
}
155