Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.55% |
52 / 55 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
Message | |
94.55% |
52 / 55 |
|
87.50% |
7 / 8 |
30.15 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasData | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
6 | |||
getJsonCompressLevel | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
getGraphQL | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
setUpdatedMetaData | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
4.84 | |||
getStatuscode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
jsonSerialize | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 | |||
getProfilerData | n/a |
0 / 0 |
n/a |
0 / 0 |
5 |
1 | <?php |
2 | |
3 | namespace BO\Zmsapi\Response; |
4 | |
5 | use BO\Zmsdb\Connection\Select; |
6 | use BO\Zmsclient\GraphQL\GraphQLInterpreter; |
7 | |
8 | /** |
9 | * example class to generate a response |
10 | */ |
11 | class Message implements \JsonSerializable |
12 | { |
13 | /** |
14 | * @var \BO\Zmsentities\Metaresult $meta |
15 | */ |
16 | public $meta = null; |
17 | |
18 | /** |
19 | * @var Mixed $data |
20 | */ |
21 | public $data = null; |
22 | |
23 | /** |
24 | * @var Mixed $data |
25 | */ |
26 | public $statuscode = 200; |
27 | |
28 | |
29 | /** |
30 | * @var \Psr\Http\Message\RequestInterface $request; |
31 | * |
32 | */ |
33 | protected $request = null; |
34 | |
35 | protected function __construct(\Psr\Http\Message\RequestInterface $request) |
36 | { |
37 | $this->request = $request; |
38 | $this->meta = new \BO\Zmsentities\Metaresult(); |
39 | $this->meta->error = false; |
40 | $this->meta->exception = null; |
41 | $this->setUpdatedMetaData(); |
42 | } |
43 | |
44 | public static function create(\Psr\Http\Message\RequestInterface $request) |
45 | { |
46 | return new self($request); |
47 | } |
48 | |
49 | public function hasData() |
50 | { |
51 | return ( |
52 | ($this->data instanceof \BO\Zmsentities\Schema\Entity && $this->data->hasId()) |
53 | || ($this->data instanceof \BO\Zmsentities\Collection\Base && count($this->data)) |
54 | || (is_array($this->data) && count($this->data)) |
55 | ); |
56 | } |
57 | |
58 | protected function getJsonCompressLevel() |
59 | { |
60 | $jsonCompressLevel = 0; |
61 | $validator = $this->request->getAttribute('validator'); |
62 | if ($validator) { |
63 | $jsonCompressLevel = $validator->getParameter('compress') |
64 | ->isNumber() |
65 | ->setDefault(0) |
66 | ->getValue(); |
67 | } |
68 | $header = intval($this->request->getHeaderLine('X-JsonCompressLevel')); |
69 | return ($header) ? $header : $jsonCompressLevel; |
70 | } |
71 | |
72 | protected function getGraphQL() |
73 | { |
74 | $validator = $this->request->getAttribute('validator'); |
75 | if ($validator) { |
76 | $gqlString = $validator->getParameter('gql') |
77 | ->isString() |
78 | ->getValue(); |
79 | if ($gqlString) { |
80 | $graphqlInterpreter = new GraphQLInterpreter($gqlString); |
81 | return $graphqlInterpreter; |
82 | } |
83 | } |
84 | return null; |
85 | } |
86 | |
87 | /** |
88 | * Update meta-data |
89 | * check for data in response |
90 | * |
91 | */ |
92 | public function setUpdatedMetaData() |
93 | { |
94 | $this->meta->generated = date('c'); |
95 | $version = \BO\Zmsapi\Helper\Version::getString(); |
96 | $this->meta->server = \App::IDENTIFIER . ' (' . $version . ')'; |
97 | if ($this->data !== null && $this->statuscode == 200 && !$this->hasData()) { |
98 | $this->statuscode = 404; |
99 | $this->meta->error = true; |
100 | $this->meta->message = 'Not found'; |
101 | } |
102 | return $this; |
103 | } |
104 | |
105 | public function getStatuscode() |
106 | { |
107 | return $this->statuscode; |
108 | } |
109 | |
110 | public function jsonSerialize() |
111 | { |
112 | $schema = $this->request->getUri()->getScheme(); |
113 | $schema .= '://'; |
114 | $schema .= $this->request->getUri()->getHost(); |
115 | $schema .= \App::$slim->urlFor('index'); |
116 | $jsonCompressLevel = $this->getJsonCompressLevel(); |
117 | |
118 | if ($jsonCompressLevel > 0 && $this->data && is_object($this->data)) { |
119 | $this->data->setJsonCompressLevel($jsonCompressLevel); |
120 | } |
121 | $graphqlInterpreter = $this->getGraphQL(); |
122 | if ($graphqlInterpreter) { |
123 | $this->data = $graphqlInterpreter->setJson(json_encode($this->data)); |
124 | } |
125 | $message = [ |
126 | '$schema' => $schema, |
127 | "meta" => $this->meta, |
128 | "data" => $this->data, |
129 | ]; |
130 | if (\App::DEBUG) { |
131 | // @codeCoverageIgnoreStart |
132 | $message['profiler'] = $this->getProfilerData(); |
133 | // @codeCoverageIgnoreEnd |
134 | } |
135 | return $message; |
136 | } |
137 | |
138 | /** |
139 | * @codeCoverageIgnore |
140 | * |
141 | */ |
142 | protected function getProfilerData() |
143 | { |
144 | $profiler = null; |
145 | if (Select::hasWriteConnection()) { |
146 | $profiler = Select::getWriteConnection()->getProfiler(); |
147 | } |
148 | if (Select::hasReadConnection()) { |
149 | $profiler = Select::getReadConnection()->getProfiler(); |
150 | } |
151 | |
152 | if ($profiler === null) { |
153 | return []; |
154 | } |
155 | |
156 | $logger = $profiler->getLogger(); |
157 | |
158 | if (method_exists($logger, 'getMessages')) { |
159 | return $logger->getMessages(); |
160 | } |
161 | |
162 | return []; |
163 | } |
164 | } |