Completed
Pull Request — master (#76)
by Thibaud
02:50
created

ApiClient::isExtendedModeEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Phraseanet-PHP-SDK.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhraseanetSDK\Http;
13
14
use PhraseanetSDK\Exception\RuntimeException;
15
16
class ApiClient
17
{
18
    /**
19
     * @var Client
20
     */
21
    private $client;
22
23
    /**
24
     * @var bool
25
     */
26
    private $extendedMode = false;
27
28
    /**
29
     * @param Client $client
30
     * @param bool $extendedMode
31
     */
32
    public function __construct(Client $client, $extendedMode = false)
33
    {
34
        $this->client = $client;
35
        $this->extendedMode = (bool) $extendedMode;
36
    }
37
38
    /**
39
     * Enables extended API responses
40
     */
41
    public function enableExtendedMode()
42
    {
43
        $this->extendedMode = true;
44
    }
45
46
    /**
47
     * Disables extended API responses
48
     */
49
    public function disableExtendedMode()
50
    {
51
        $this->extendedMode = false;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function isExtendedModeEnabled()
58
    {
59
        return $this->extendedMode;
60
    }
61
62
    /**
63
     * @return Client
64
     */
65
    public function getClient()
66
    {
67
        return $this->client;
68
    }
69
70
    /**
71
     * @param string $method
72
     * @param string $path
73
     * @param array $query
74
     * @param array $postFields
75
     * @param array $files
76
     * @param array $headers
77
     * @return ApiResponse
78
     */
79
    public function call(
80
        $method,
81
        $path,
82
        array $query = array(),
83
        array $postFields = array(),
84
        array $files = array(),
85
        array $headers = array()
86
    ) {
87
        $headers = array_replace($headers, [ 'Accept' => $this->getAcceptHeader() ]);
88
        $responseBody = $this->client->call($method, $path, $query, $postFields, $files, $headers);
89
90
        $decodedResponse = @json_decode($responseBody);
91
92
        if (JSON_ERROR_NONE !== json_last_error()) {
93
            throw new RuntimeException(
94
                'Json response cannot be decoded or the encoded data is deeper than the recursion limit'
95
            );
96
        }
97
98
        return new ApiResponse($decodedResponse);
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    private function getAcceptHeader()
105
    {
106
        return $this->isExtendedModeEnabled() ?
107
            'application/vnd.phraseanet.record-extended+json' :
108
            'application/json';
109
    }
110
}
111