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