Issues (1574)

src/Http/Middleware/SoapActionMiddleware.php (12 issues)

1
<?php
2
0 ignored issues
show
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
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @author tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
15
class SoapActionMiddleware implements MiddlewareInterface
16
{
17
    /**
0 ignored issues
show
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Parameter $handler should have a doc-comment as per coding-style.
Loading history...
18
     * {@inheritDoc}
19
     */
0 ignored issues
show
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
Missing short description in doc comment
Loading history...
26
     * @param RequestInterface $request
0 ignored issues
show
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
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
}