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

Response   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 48
ccs 0
cts 16
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getResponseValue() 0 16 4
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\Resources;
20
21
use phparia\Client\AriClient;
22
use phparia\Exception\InvalidParameterException;
23
24
/**
25
 * Base type for API call responses
26
 *
27
 * @author Brian Smith <[email protected]>
28
 */
29
class Response
30
{
31
    /**
32
     * The json_decoded message data from ARI
33
     *
34
     * @var object
35
     */
36
    protected $response;
37
38
    /**
39
     * @param string $response The raw json response response data from ARI
40
     */
41
    public function __construct($response)
42
    {
43
        if (is_array($response)) { // For some reason, playback is an array, so this fixes that problem
44
            $this->response = json_decode(json_encode($response), false);
45
        } elseif (is_object($response)) {
46
            $this->response = $response;
47
        } else {
48
            $this->response = json_decode($response);
49
        }
50
    }
51
52
    /**
53
     * Get the response value or object depending on which type of class.
54
     *
55
     * @param string $propertyName The name of the property to retrieve
56
     * @param string|null $class (optional) The name of the class to pass the propertyValue to and return
57
     * @param AriClient|null $client (optional, requires $class) The AriClient instance to pass to the class in the case of Resource types
58
     * @return array|mixed
59
     */
60
    protected function getResponseValue($propertyName, $class = null, AriClient $client = null)
61
    {
62
        if (property_exists($this->response, $propertyName)) {
63
            if ($class !== null) {
64
                if ($client instanceof AriClient) {
65
                    return new $class($client, $this->response->{$propertyName});
66
                } else {
67
                    return new $class($this->response->{$propertyName});
68
                }
69
            } else {
70
                return $this->response->{$propertyName};
71
            }
72
        } else {
73
            return null;
74
        }
75
    }
76
}
77