SoapCallRegistry   A
last analyzed

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