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