Completed
Push — master ( 5acb47...9425df )
by Ivo
02:12 queued 14s
created

SoapCallRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 68
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addRequest() 0 12 1
A addResponse() 0 13 2
A prettyXML() 0 15 2
A getCalls() 0 4 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