ApiInternalController::getInternalRouter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Napp\Core\Api\Controllers;
4
5
use Illuminate\Auth\AuthManager;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Response;
8
use Napp\Core\Api\Auth\NappHttpHeaders;
9
use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallException;
10
use Napp\Core\Api\Router\Router;
11
12
/**
13
 * Class ApiInternalController.
14
 */
15
class ApiInternalController extends BaseController
16
{
17
    /**
18
     * @var AuthManager
19
     */
20
    protected $auth;
21
22
    /**
23
     * @var Router
24
     */
25
    protected $internalApiRouter;
26
27
    /**
28
     * @param AuthManager $auth
29
     * @param Router      $router
30
     */
31
    public function __construct(AuthManager $auth, Router $router)
32
    {
33
        $this->auth = $auth;
34
        $this->internalApiRouter = $router;
35
    }
36
37
    /**
38
     * @return Router
39
     */
40
    public function getInternalRouter(): Router
41
    {
42
        return $this->internalApiRouter;
43
    }
44
45
    /**
46
     * @param string $uri
47
     * @param array  $headers
48
     *
49
     * @return array
50
     */
51
    public function get(string $uri, array $headers = []): array
52
    {
53
        return $this->formatResponse(
54
            $this->getInternalRouter()->get($uri, [], $this->getInternalCallHeaders($headers))
55
        );
56
    }
57
58
    /**
59
     * @param string $uri
60
     * @param array  $data
61
     * @param array  $headers
62
     *
63
     * @return array
64
     */
65
    public function post(string $uri, array $data, array $headers = []): array
66
    {
67
        return $this->formatResponse(
68
            $this->getInternalRouter()->post($uri, $data, $this->getInternalCallHeaders($headers))
69
        );
70
    }
71
72
    /**
73
     * @param string $uri
74
     * @param array  $data
75
     * @param array  $headers
76
     *
77
     * @return array
78
     */
79
    public function put(string $uri, array $data, array $headers = []): array
80
    {
81
        return $this->formatResponse(
82
            $this->getInternalRouter()->put($uri, $data, $this->getInternalCallHeaders($headers))
83
        );
84
    }
85
86
    /**
87
     * @param string $uri
88
     * @param array  $headers
89
     *
90
     * @return array
91
     */
92
    public function delete(string $uri, array $headers = []): array
93
    {
94
        return $this->formatResponse(
95
            $this->getInternalRouter()->delete($uri, [], $this->getInternalCallHeaders($headers))
96
        );
97
    }
98
99
    /**
100
     * @param array $headers
101
     *
102
     * @return array
103
     */
104
    protected function getInternalCallHeaders(array $headers): array
105
    {
106
        return array_merge($headers, [
107
            NappHttpHeaders::NAPP_API_CALL_TYPE       => 'internal',
108
            NappHttpHeaders::NAPP_AUTH_GLOBAL_USER_ID => $this->auth->guard()->id(),
109
        ]);
110
    }
111
112
    /**
113
     * @param Response|JsonResponse $response
114
     *
115
     * @throws ApiInternalCallException
116
     *
117
     * @return array
118
     */
119
    protected function formatResponse($response): array
120
    {
121
        if (true === $response instanceof JsonResponse) {
122
            $data = $response->getData(true);
123
        } else {
124
            $data = json_decode($response->getContent(), true);
125
        }
126
127
        if (true === array_key_exists('error', $data)) {
128
            if (true === config('app.debug')) {
129
                $message = json_encode($data['error']);
130
            } else {
131
                $message = $data['error']['message'];
132
            }
133
134
            throw new ApiInternalCallException($response, $message);
135
        }
136
137
        return $data;
138
    }
139
}
140