Completed
Push — master ( bac1bc...5acb47 )
by Ivo
07:35
created

SoapCallRegistry::addRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 12
cp 0.75
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.0156
1
<?php
2
3
namespace Freshcells\SoapClientBundle\DataCollector;
4
5
use Freshcells\SoapClientBundle\Event\RequestEvent;
6
use Freshcells\SoapClientBundle\Event\ResponseEvent;
7
8
class SoapCallRegistry
9
{
10
    /**
11
     * @var array
12
     */
13
    private $calls = [];
14
15
    /**
16
     * @param RequestEvent $event
17
     */
18 6
    public function addRequest(RequestEvent $event)
19
    {
20
        $request                      = array(
21 6
            'resource'         => $event->getResource(),
22 6
            'request_headers'  => '',
23 6
            'request_body'     => $this->prettyXML($event->getRequest()),
24 6
            'response_headers' => '',
25 6
            'response_body'    => '',
26 6
            'start'            => microtime(true),
27
        );
28 6
        $this->calls[$event->getId()] = $request;
29 6
    }
30
31
    /**
32
     * @param ResponseEvent $event
33
     */
34 6
    public function addResponse(ResponseEvent $event)
35
    {
36 6
        $id                                   = $event->getId();
37 6
        $response                             = $event->getResponseContent() ?
38 6
            $this->prettyXML($event->getResponseContent()) :
39 6
            $event->getResponseContent();
40 6
        $this->calls[$id]['end']              = microtime(true);
41 6
        $this->calls[$id]['request_headers']  = $event->getRequestHeaders();
42 6
        $this->calls[$id]['request_body']     = $this->prettyXML($event->getRequestContent());
43 6
        $this->calls[$id]['response_headers'] = $event->getResponseHeaders();
44 6
        $this->calls[$id]['response_body']    = $response;
45 6
        $this->calls[$id]['duration']         = ($this->calls[$id]['end'] - $this->calls[$id]['start']);
46 6
    }
47
48
    /**
49
     * @param string $xml
50
     * @return string
51
     */
52 6
    private function prettyXML(string $xml): string
53
    {
54
        try {
55
            //nicen
56 6
            $doc = new \DomDocument('1.0');
57 6
            $doc->loadXML($xml);
58 4
            $doc->formatOutput = true;
59
60 4
            return $doc->saveXML();
61 2
        } catch (\Exception $e) {
62
            // probably no xml, just let it pass
63
        }
64
65 2
        return $xml;
66
    }
67
68
    /**
69
     * @return array
70
     */
71 4
    public function getCalls(): array
72
    {
73 4
        return $this->calls;
74
    }
75
}
76