SoapActionMiddleware::withSoapHeader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.9332
cc 2
nc 2
nop 1
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace DMT\Insolvency\Http\Middleware;
4
5
use DMT\Http\Client\MiddlewareInterface;
6
use DMT\Http\Client\RequestHandlerInterface;
7
use DMT\Insolvency\Exception\RequestException;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use SimpleXMLElement;
11
12
/**
13
 * Class SoapActionMiddleware
14
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
15
class SoapActionMiddleware implements MiddlewareInterface
16
{
17
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $handler should have a doc-comment as per coding-style.
Loading history...
18
     * {@inheritDoc}
19
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
20 12
    public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
21
    {
22 12
        return $handler->handle($this->withSoapHeader($request));
23
    }
24
25
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
     * @param RequestInterface $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
27
     *
28
     * @return RequestInterface
29
     * @throws RequestException
30
     */
31 12
    private function withSoapHeader(RequestInterface $request): RequestInterface
0 ignored issues
show
Coding Style introduced by
Private method name "SoapActionMiddleware::withSoapHeader" must be prefixed with an underscore
Loading history...
32
    {
33 12
        $request->getBody()->rewind();
34
35 12
        $xml = new SimpleXMLElement($request->getBody()->getContents());
36
37 12
        $query = '//*[local-name()="Action" and namespace-uri()="http://schemas.xmlsoap.org/ws/2004/08/addressing"]';
38 12
        $elements = $xml->xpath($query);
39
40 12
        if (count($elements) === 0) {
41 1
            throw new RequestException(
42 1
                'Malformed request, missing {http://schemas.xmlsoap.org/ws/2004/08/addressing}Action'
43
            );
44
        }
45
46
        return $request
47 11
            ->withAddedHeader('SOAPAction', strval($elements[0]))
48 11
            ->withAddedHeader('Content-Type', 'text/xml; charset=utf-8');
49
    }
50
}