Completed
Push — master ( 3b0a50...3c7c0d )
by Giancarlos
03:54
created

BaseSunat   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 85
ccs 34
cts 34
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 4 1
A setClient() 0 5 1
A extractResponse() 0 8 1
A getMessageError() 0 7 1
B getErrorFromFault() 0 28 5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 15/07/2017
6
 * Time: 23:13
7
 */
8
9
namespace Greenter\Ws\Services;
10
11
use Greenter\Model\Response\Error;
12
use Greenter\Ws\Reader\DomCdrReader;
13
use Greenter\Ws\Reader\XmlErrorReader;
14
use Greenter\Zip\ZipFactory;
15
16
/**
17
 * Class BaseSunat
18
 * @package Greenter\Ws\Services
19
 */
20
class BaseSunat
21
{
22
    /**
23
     * @var WsClientInterface
24
     */
25
    private $client;
26
27
    /**
28
     * @return WsClientInterface
29
     */
30 44
    public function getClient()
31
    {
32 44
        return $this->client;
33
    }
34
35
    /**
36
     * @param WsClientInterface $client
37
     * @return BaseSunat
38
     */
39 62
    public function setClient($client)
40
    {
41 62
        $this->client = $client;
42 62
        return $this;
43
    }
44
45
    /**
46
     * Get error from Fault Exception.
47
     *
48
     * @param \SoapFault $fault
49
     * @return Error
50
     */
51 20
    protected function getErrorFromFault(\SoapFault $fault)
52
    {
53 20
        $err = new Error();
54 20
        $fcode = $fault->faultcode;
0 ignored issues
show
Bug introduced by
The property faultcode does not seem to exist in SoapFault.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55 20
        $code = preg_replace('/[^0-9]+/', '', $fcode);
56 20
        $msg = '';
57
58 20
        if ($code) {
59 14
            $msg = $this->getMessageError($code);
60 14
            $fcode = $code;
61 14
        } else {
62 6
            $code = preg_replace('/[^0-9]+/', '', $fault->faultstring);
0 ignored issues
show
Bug introduced by
The property faultstring does not seem to exist. Did you mean string?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
64 6
            if ($code) {
65 4
                $msg = $this->getMessageError($code);
66 4
                $fcode = $code;
67 4
            }
68
        }
69
70 20
        if (!$msg) {
71 4
            $msg = isset($fault->detail) ? $fault->detail->message : $fault->faultstring;
0 ignored issues
show
Bug introduced by
The property detail does not seem to exist in SoapFault.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property faultstring does not seem to exist. Did you mean string?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
72 4
        }
73
74 20
        $err->setCode($fcode);
75 20
        $err->setMessage($msg);
76
77 20
        return $err;
78
    }
79
80
    /**
81
     * @param $zipContent
82
     * @return \Greenter\Model\Response\CdrResponse
83
     */
84 18
    protected function extractResponse($zipContent)
85
    {
86 18
        $zip = new ZipFactory();
87 18
        $xml = $zip->decompressLastFile($zipContent);
88 18
        $reader = new DomCdrReader();
89
90 18
        return $reader->getCdrResponse($xml);
91
    }
92
93
    /**
94
     * @param $code
95
     * @return string
96
     */
97 18
    protected function getMessageError($code)
98
    {
99 18
        $search = new XmlErrorReader();
100 18
        $msg = $search->getMessageByCode(intval($code));
101
102 18
        return $msg;
103
    }
104
}