ServiceRestTransport   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parentErrorResponse() 0 3 1
A callLogic() 0 16 3
A outputException() 0 5 1
A callRoute() 0 3 1
A errorResponse() 0 18 3
A callPublicLogic() 0 16 3
1
<?php
2
namespace Mezon\Service\ServiceRestTransport;
3
4
/**
5
 * Class ServiceRestTransport
6
 *
7
 * @package Service
8
 * @subpackage ServiceRestTransport
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/08/17)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * REST transport for all services.
16
 *
17
 * @author Dodonov A.A.
18
 */
19
class ServiceRestTransport extends \Mezon\Service\ServiceHttpTransport\ServiceHttpTransport
20
{
21
22
    /**
23
     * Method runs logic functions.
24
     *
25
     * @param \Mezon\Service\ServiceBaseLogicInterface $serviceLogic
26
     *            -
27
     *            object with all service logic.
28
     * @param string $method
29
     *            -
30
     *            logic's method to be executed.
31
     * @param array $params
32
     *            -
33
     *            logic's parameters.
34
     * @return mixed Result of the called method.
35
     */
36
    public function callLogic(\Mezon\Service\ServiceBaseLogicInterface $serviceLogic, string $method, array $params = [])
37
    {
38
        $this->header('Content-Type', 'application/json');
39
40
        try {
41
            $params['SessionId'] = $this->createSession($this->getParamsFetcher()
42
                ->getParam('session_id'));
43
44
            return call_user_func_array([
45
                $serviceLogic,
46
                $method
47
            ], $params);
48
        } catch (\Mezon\Rest\Exception $e) {
49
            return $this->errorResponse($e);
50
        } catch (\Exception $e) {
51
            return $this->parentErrorResponse($e);
52
        }
53
    }
54
55
    /**
56
     * Method runs logic functions.
57
     *
58
     * @param \Mezon\Service\ServiceBaseLogicInterface $serviceLogic
59
     *            -
60
     *            object with all service logic.
61
     * @param string $method
62
     *            -
63
     *            logic's method to be executed.
64
     * @param array $params
65
     *            -
66
     *            logic's parameters.
67
     * @return mixed Result of the called method.
68
     */
69
    public function callPublicLogic(
70
        \Mezon\Service\ServiceBaseLogicInterface $serviceLogic,
71
        string $method,
72
        array $params = [])
73
    {
74
        $this->header('Content-Type', 'application/json');
75
76
        try {
77
            return call_user_func_array([
78
                $serviceLogic,
79
                $method
80
            ], $params);
81
        } catch (\Mezon\Rest\Exception $e) {
82
            return $this->errorResponse($e);
83
        } catch (\Exception $e) {
84
            return $this->parentErrorResponse($e);
85
        }
86
    }
87
88
    /**
89
     * Method calls route in transport specific way
90
     */
91
    protected function callRoute(): void
92
    {
93
        print(json_encode($this->getRouter()->callRoute($_GET['r'])));
94
    }
95
96
    /**
97
     * Error response compilator
98
     *
99
     * @param mixed $e
100
     *            Exception object
101
     * @return array Error data
102
     */
103
    public function errorResponse($e): array
104
    {
105
        $return = [
106
            'message' => $e->getMessage(),
107
            'code' => $e->getCode(),
108
            'service' => $_SERVER['HTTP_HOST'] ?? 'unknown'
109
        ];
110
111
        if ($this->isDebug()) {
112
            $return['call_stack'] = $this->formatCallStack($e);
113
        }
114
115
        if ($e instanceof \Mezon\Rest\Exception) {
116
            $return['http_code'] = $e->getHttpCode();
117
            $return['http_body'] = $e->getHttpBody();
118
        }
119
120
        return $return;
121
    }
122
123
    /**
124
     * Error response compilator
125
     *
126
     * @param mixed $e
127
     *            Exception object
128
     * @return array Error data
129
     */
130
    public function parentErrorResponse($e): array
131
    {
132
        return parent::errorResponse($e);
133
    }
134
135
    /**
136
     * Method outputs exception data
137
     *
138
     * @param array $e
139
     *            exception data
140
     */
141
    public function outputException(array $e): void
142
    {
143
        $this->header('Content-Type', 'application/json');
144
145
        print(json_encode($e));
146
    }
147
}
148