Lines
100.00%
35 / 35
Functions and Methods
100.00%
8 / 8
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| TwigExtension | 100.00% | 35 / 35 | 100.00% | 8 / 8 | 14 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getName | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getFunctions | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| dumpHttpLog | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | |||||
| parseBody | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 3 | |||||
| parseHeaders | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| formatRequest | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| formatResponse | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | /** | |
| 4 | * @package BO Slim | |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG | |
| 6 | **/ | |
| 7 | ||
| 8 | namespace BO\Zmsclient; | |
| 9 | ||
| 10 | use BO\Mellon\Validator; | |
| 11 | ||
| 12 | /** | |
| 13 | * Extension for Twig and Slim | |
| 14 | * | |
| 15 | * @SuppressWarnings(PublicMethod) | |
| 16 | * @SuppressWarnings(TooManyMethods) | |
| 17 | * @SuppressWarnings(UnusedPrivateField) | |
| 18 | */ | |
| 19 | class TwigExtension extends \Twig\Extension\AbstractExtension | |
| 20 | { | |
| 21 | /** | |
| 22 | * @var mixed | |
| 23 | */ | |
| 24 | private mixed $container; | |
| 25 | ||
| 26 | public function __construct(mixed $container) | |
| 27 | { | |
| 28 | $this->container = $container; | |
| 29 | } | |
| 30 | ||
| 31 | public function getName(): string | |
| 32 | { | |
| 33 | return 'bozmsclientExtension'; | |
| 34 | } | |
| 35 | ||
| 36 | #[\Override] | |
| 37 | public function getFunctions() | |
| 38 | { | |
| 39 | $safe = array('is_safe' => array('html')); | |
| 40 | return array( | |
| 41 | new \Twig\TwigFunction('dumpHttpLog', array($this, 'dumpHttpLog'), $safe), | |
| 42 | ); | |
| 43 | } | |
| 44 | ||
| 45 | public function dumpHttpLog(): string | |
| 46 | { | |
| 47 | $output = '<h2>HTTP API-Log</h2>' | |
| 48 | . ' <p>For debugging: This log contains HTTP calls. <strong>DISABLE FOR PRODUCTION!</strong></p>'; | |
| 49 | foreach (Http::$log as $entry) { | |
| 50 | if ($entry instanceof \Psr\Http\Message\RequestInterface) { | |
| 51 | $entry = $this->formatRequest($entry); | |
| 52 | } elseif ($entry instanceof \Psr\Http\Message\ResponseInterface) { | |
| 53 | $entry = $this->formatResponse($entry); | |
| 54 | } | |
| 55 | ||
| 56 | $output .= \Tracy\Debugger::dump($entry, true); | |
| 57 | } | |
| 58 | return $output; | |
| 59 | } | |
| 60 | ||
| 61 | protected function parseBody(\Psr\Http\Message\StreamInterface $body, bool $allowEmpty = false): mixed | |
| 62 | { | |
| 63 | $content = Validator::value((string)$body)->isJson(); | |
| 64 | if ($content->hasFailed() && !$allowEmpty) { | |
| 65 | $output = | |
| 66 | 'API-Call failed, JSON parsing with error: ' . (string) ($content->getMessages() ?? '') | |
| 67 | . ' - Snippet: ' . substr(\strip_tags((string)$body), 0, 2000) . '...' | |
| 68 | ; | |
| 69 | } else { | |
| 70 | $output = $content->getValue(); | |
| 71 | } | |
| 72 | return $output; | |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * @return string[] | |
| 77 | * | |
| 78 | */ | |
| 79 | protected function parseHeaders(\Psr\Http\Message\MessageInterface $message): array | |
| 80 | { | |
| 81 | $headers = []; | |
| 82 | foreach ($message->getHeaders() as $name => $header) { | |
| 83 | $headers[$name] = implode("|doubled|", $header); | |
| 84 | } | |
| 85 | return $headers; | |
| 86 | } | |
| 87 | ||
| 88 | protected function formatRequest(\Psr\Http\Message\RequestInterface $request): array | |
| 89 | { | |
| 90 | $output = []; | |
| 91 | $output['header'] = $this->parseHeaders($request); | |
| 92 | $output['body'] = $this->parseBody($request->getBody(), true); | |
| 93 | return $output; | |
| 94 | } | |
| 95 | ||
| 96 | protected function formatResponse(\Psr\Http\Message\ResponseInterface $response): array | |
| 97 | { | |
| 98 | $output = []; | |
| 99 | $output['header'] = $this->parseHeaders($response); | |
| 100 | $output['body'] = $this->parseBody($response->getBody()); | |
| 101 | return $output; | |
| 102 | } | |
| 103 | } |