Passed
Push — master ( 2944ee...dfe2d2 )
by Asmir
02:04
created

Fault::fromException()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 43
rs 8.5546
cc 7
nc 32
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GoetasWebservices\SoapServices\Metadata\Envelope\SoapEnvelope12\Messages;
6
7
use GoetasWebservices\SoapServices\Metadata\Envelope\SoapEnvelope12\Parts\Fault as FaultPart;
8
use GoetasWebservices\SoapServices\Metadata\Envelope\SoapEnvelope12\Parts\FaultCode;
9
use GoetasWebservices\SoapServices\Metadata\Exception\Fault12Exception;
0 ignored issues
show
Bug introduced by
The type GoetasWebservices\SoapSe...eption\Fault12Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use GoetasWebservices\SoapServices\Metadata\Exception\FaultException as FaultExceptionMeta;
0 ignored issues
show
Bug introduced by
The type GoetasWebservices\SoapSe...xception\FaultException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use GoetasWebservices\SoapServices\SoapServer\Exception\ClientException;
0 ignored issues
show
Bug introduced by
The type GoetasWebservices\SoapSe...ception\ClientException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use GoetasWebservices\SoapServices\SoapServer\Exception\FaultException;
0 ignored issues
show
Bug introduced by
The type GoetasWebservices\SoapSe...xception\FaultException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use GoetasWebservices\SoapServices\SoapServer\Exception\MustUnderstandException;
0 ignored issues
show
Bug introduced by
The type GoetasWebservices\SoapSe...MustUnderstandException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use GoetasWebservices\SoapServices\SoapServer\Exception\ServerException;
0 ignored issues
show
Bug introduced by
The type GoetasWebservices\SoapSe...ception\ServerException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use GoetasWebservices\SoapServices\SoapServer\Exception\VersionMismatchException;
0 ignored issues
show
Bug introduced by
The type GoetasWebservices\SoapSe...ersionMismatchException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Psr\Http\Message\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Class representing Body
21
 */
22
class Fault
23
{
24
    /**
25
     * @var FaultBody $body
26
     */
27
    private $body = null;
28
29
    /**
30
     * Gets as body
31
     */
32
    public function getBody(): FaultBody
33
    {
34
        return $this->body;
35
    }
36
37
    /**
38
     * Sets a new body
39
     */
40
    public function setBody(FaultBody $body): self
41
    {
42
        $this->body = $body;
43
44
        return $this;
45
    }
46
47
    public static function fromException(\Throwable $e, bool $debug = false): self
48
    {
49
        $faultEnvelope = new self();
50
        $faultBody = new FaultBody();
51
        $faultEnvelope->setBody($faultBody);
52
53
        $fault = new FaultPart();
54
        if (!$e instanceof FaultException) {
55
            $e = new ServerException($e->getMessage(), $e->getCode(), $e);
56
        }
57
58
        $faultCode = new FaultCode();
59
60
        if ($e instanceof VersionMismatchException) {
61
            $faultCode->setValue('SOAP:VersionMismatch');
62
        } elseif ($e instanceof MustUnderstandException) {
63
            $faultCode->setValue('SOAP:MustUnderstand');
64
        } elseif ($e instanceof ClientException) {
65
            $faultCode->setValue('SOAP:Sender');
66
        } else {
67
            $faultCode->setValue('SOAP:Receiver');
68
        }
69
70
        if (0 !== $e->getCode()) {
71
            $subFaultCode = new FaultCode();
72
            $subFaultCode->setValue((string) $e->getCode());
73
74
            $faultCode->setSubcode($subFaultCode);
75
        }
76
77
        $fault->setCode($faultCode);
78
        if ($debug) {
79
            $fault->setReason(array_merge([$e->getMessage()], explode("\n", (string) $e)));
80
        } else {
81
            $fault->setReason(explode("\n", $e->getMessage()));
82
        }
83
84
        // @todo implement detail wrapping
85
        $fault->setDetail($e->getDetail());
0 ignored issues
show
Bug introduced by
The method getDetail() does not exist on Throwable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $fault->setDetail($e->/** @scrutinizer ignore-call */ getDetail());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
87
        $faultBody->setFault($fault);
88
89
        return $faultEnvelope;
90
    }
91
92
    /**
93
     * @param \Exception $e
94
     */
95
    public function createException(ResponseInterface $response, RequestInterface $request, ?\Throwable $e = null): FaultExceptionMeta
96
    {
97
        return new Fault12Exception($this->getBody()->getFault(), $response, $request, $e);
98
    }
99
}
100