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