Correios::getConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
namespace Eduardokum\CorreiosPhp;
3
4
use Eduardokum\CorreiosPhp\Config\Testing;
5
use Eduardokum\CorreiosPhp\Contracts\Config\Config as ConfigContract;
6
use Eduardokum\CorreiosPhp\Exception\InvalidSoapException;
7
use Eduardokum\CorreiosPhp\Soap\Soap;
8
9
abstract class Correios
10
{
11
    /**
12
     * @var \stdClass
13
     */
14
    private $ws = [];
15
16
    /**
17
     * @var ConfigContract
18
     */
19
    private $config = null;
20
21
    /**
22
     * @var Soap
23
     */
24
    private $soap = null;
25
26
    public function __construct(ConfigContract $config = null, $type = 'curl')
27
    {
28
        $this->config = $config ?: new Testing();
29
        $this->makeSoap($type);
30
        $webservices = realpath(CORREIOS_PHP_BASE) . '/storage/webservices.json';
31
        $this->setWs(json_decode(file_get_contents($webservices)));
32
    }
33
34
    /**
35
     * @param \stdClass $ws
36
     *
37
     * @return $this
38
     */
39
    protected function setWs(\stdClass $ws)
40
    {
41
        $this->ws = $ws;
42
        return $this;
43
    }
44
45
    /**
46
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
47
     *
48
     * @return \stdClass
49
     */
50
    protected function getWs($key = null)
51
    {
52
        return $key && property_exists($this->ws, $key)
0 ignored issues
show
introduced by
$key is of type null, thus it always evaluated to false.
Loading history...
Bug introduced by
$key of type void is incompatible with the type string expected by parameter $property of property_exists(). ( Ignorable by Annotation )

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

52
        return $key && property_exists($this->ws, /** @scrutinizer ignore-type */ $key)
Loading history...
53
            ? $this->ws->$key
54
            : $this->ws;
55
    }
56
57
    /**
58
     * @param $type
59
     *
60
     * @return Soap
61
     * @throws InvalidSoapException
62
     */
63
    private function makeSoap($type)
64
    {
65
        $soapClass = '\\Eduardokum\\CorreiosPhp\\Soap\\Soap' . ucfirst(strtolower($type));
66
67
        if (class_exists($soapClass) && !empty(trim($type))) {
68
            $this->soap = new $soapClass();
69
            return $this->soap;
70
        }
71
72
        throw new InvalidSoapException("The type '$type' is not acceptable");
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    protected function url()
79
    {
80
        $env = $this->config->getEnvironment() == 'testing' ? 'homologacao' : 'producao';
81
        return $this->ws->$env;
82
    }
83
84
    /**
85
     * @return ConfigContract
86
     */
87
    public function getConfig()
88
    {
89
        return $this->config;
90
    }
91
92
    /**
93
     * @return Soap
94
     */
95
    public function getSoap()
96
    {
97
        return $this->soap;
98
    }
99
}
100