Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
TwigExtension
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
8 / 8
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 dumpHttpLog
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 parseBody
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 parseHeaders
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 formatRequest
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 formatResponse
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @package   BO Slim
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsclient;
9
10use BO\Mellon\Validator;
11use Psr\Container\ContainerInterface;
12
13/**
14  * Extension for Twig and Slim
15  *
16  *  @SuppressWarnings(PublicMethod)
17  *  @SuppressWarnings(TooManyMethods)
18  */
19class 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(): 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)
62    {
63        $content = Validator::value((string)$body)->isJson();
64        if ($content->hasFailed() && !$allowEmpty) {
65            $output =
66                'API-Call failed, JSON parsing with error: ' . $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}