Completed
Push — master ( 5acb47...9425df )
by Ivo
02:12 queued 14s
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

Importance

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