AbstractResponse   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 29.63%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 21
c 2
b 0
f 0
dl 0
loc 81
ccs 8
cts 27
cp 0.2963
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parseContents() 0 2 1
A getError() 0 3 1
A setError() 0 3 1
A hasError() 0 3 1
A getContents() 0 3 1
A __construct() 0 23 4
1
<?php
2
3
namespace Graze\WipotecCheckweigherClient\Response;
4
5
use Exception;
6
use Graze\TelnetClient\TelnetResponseInterface;
7
use Graze\XmlUtils\XmlConverter;
8
use SimpleXMLElement;
9
10
class AbstractResponse implements ResponseInterface
11
{
12
    /** @var mixed[] */
13
    private $contents;
14
15
    /** @var string */
16
    private $error;
17
18
    /**
19
     * @param TelnetResponseInterface $telnetResponse
20
     */
21 2
    public function __construct(TelnetResponseInterface $telnetResponse)
22
    {
23
        try {
24 2
            $xml = @new SimpleXMLElement($telnetResponse->getResponseText(), 0, false, 'cw', true);
25 2
        } catch (Exception $e) {
26 2
            $this->setError($e->getMessage());
27 2
            return;
28
        }
29
30
        $xmlConverter = new XmlConverter();
31
        $this->contents = $xmlConverter->convertToArray($xml);
32
33
        if ($telnetResponse->isError()) {
34
            $error = 'unknown';
35
            if (isset($this->contents['error_info']['error_text'])) {
36
                $error = $this->contents['error_info']['error_text'];
37
            }
38
39
            $this->setError('Telnet response error: '.$error);
40
            return;
41
        }
42
43
        $this->parseContents($this->contents);
44
    }
45
46
    /**
47
     * @param string $error
48
     */
49 2
    private function setError($error)
50
    {
51 2
        $this->error = $error;
52 2
    }
53
54
    /**
55
     * Whether an error was returned.
56
     *
57
     * @return bool
58
     */
59
    public function hasError()
60
    {
61
        return (bool)$this->error;
62
    }
63
64
    /**
65
     * Get the error message.
66
     *
67
     * @return string
68
     */
69
    public function getError()
70
    {
71
        return $this->error;
72
    }
73
74
    /**
75
     * Extract specific information from the raw contents array for easier access.
76
     *
77
     * @param mixed[] $contents
78
     */
79
    protected function parseContents(array $contents)
0 ignored issues
show
Unused Code introduced by
The parameter $contents is not used and could be removed. ( Ignorable by Annotation )

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

79
    protected function parseContents(/** @scrutinizer ignore-unused */ array $contents)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
    }
82
83
    /**
84
     * Get the raw response as an array.
85
     *
86
     * @return mixed[]
87
     */
88
    public function getContents()
89
    {
90
        return $this->contents;
91
    }
92
}
93