Passed
Push — master ( 18fc83...7dcdd4 )
by Giancarlos
06:09
created

BaseSunat   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 4 1
A setClient() 0 5 1
A getMessageError() 0 7 1
B getErrorFromFault() 0 22 5
A extractResponse() 0 8 1
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 74
    public function setClient($client)
40
    {
41 74
        $this->client = $client;
42 74
        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
        $err->setCode($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]+/', '', $err->getCode());
56 20
        $msg = '';
57
58 20
        if (empty($code)) {
59 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...
60 6
        }
61
62 20
        if ($code) {
63 18
            $msg = $this->getMessageError($code);
64 18
            $err->setCode($code);
65 18
        }
66
67 20
        if (empty($msg)) {
68 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...
69 4
        }
70
71 20
        return $err->setMessage($msg);
72
    }
73
74
    /**
75
     * @param $zipContent
76
     * @return \Greenter\Model\Response\CdrResponse
77
     */
78 18
    protected function extractResponse($zipContent)
79
    {
80 18
        $zip = new ZipFactory();
81 18
        $xml = $zip->decompressLastFile($zipContent);
82 18
        $reader = new DomCdrReader();
83
84 18
        return $reader->getCdrResponse($xml);
85
    }
86
87
    /**
88
     * @param $code
89
     * @return string
90
     */
91 18
    protected function getMessageError($code)
92
    {
93 18
        $search = new XmlErrorReader();
94 18
        $msg = $search->getMessageByCode(intval($code));
95
96 18
        return $msg;
97
    }
98
}