Completed
Push — master ( f18332...65e840 )
by Gabriel
03:52
created

Request   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 112
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetch() 0 20 2
A getBody() 0 18 2
A setTemplate() 0 4 1
A getId() 0 4 1
A setId() 0 4 1
A getCustomerId() 0 4 1
A setCustomerId() 0 4 1
A getFacilityId() 0 4 1
A setFacilityId() 0 4 1
A getLogin() 0 4 1
A setLogin() 0 4 1
A getPassword() 0 4 1
A setPassword() 0 4 1
1
<?php
2
3
namespace ThreePlCentral;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7;
7
8
class Request implements RequestInterface
9
{
10
    private $id;
11
    private $customerId;
12
    private $facilityId;
13
    private $login;
14
    private $password;
15
    private $method;
16
    private $url;
17
    private $template;
18
19
    public function __construct(string $method, string $url)
20
    {
21
        $this->method = $method;
22
        $this->url = $url;
23
    }
24
25
    public function fetch(array $data): ResponseInterface
26
    {
27
        $client = new Client();
28
        $request = new Psr7\Request(
29
            $this->method,
30
            'https://secure-wms.com/webserviceexternal/contracts.asmx',
31
            [
32
                'SOAPAction' => $this->url,
33
                'Content-Type' => 'text/xml; charset=utf-8'
34
            ],
35
            $this->getBody($data)
36
        );
37
38
        try {
39
            $reponse = $client->send($request);
40
            var_dump($reponse);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($reponse); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
41
        } catch (\Exception $error) {
42
            throw new Exception($error->getMessage(), $error->getCode(), $error);
43
        }
44
    }
45
46
    private function getBody(array $data): string
47
    {
48
        $content = file_get_contents($this->template);
49
50
        $data = array_merge($data, [
51
          'ThreePLID' => $this->getId(),
52
          'Login' => $this->getLogin(),
53
          'Password' => $this->getPassword(),
54
          'CustomerID' => $this->getCustomerId(),
55
          'FacilityID' => $this->getFacilityId()
56
        ]);
57
58
        foreach ($data as $prop => $value) {
59
            $content = str_replace("{{$prop}}", $value, $content);
60
        }
61
62
        return $content;
63
    }
64
65
    public function setTemplate(string $template)
66
    {
67
        $this->template = $template;
68
    }
69
70
    public function getId(): string
71
    {
72
        return $this->id;
73
    }
74
75
    public function setId(string $id)
76
    {
77
        $this->id = $id;
78
    }
79
80
    public function getCustomerId(): string
81
    {
82
        return $this->customerId;
83
    }
84
85
    public function setCustomerId(string $customerId)
86
    {
87
        $this->customerId = $customerId;
88
    }
89
90
    public function getFacilityId(): string
91
    {
92
        return $this->facilityId;
93
    }
94
95
    public function setFacilityId(string $facilityId)
96
    {
97
        $this->facilityId = $facilityId;
98
    }
99
100
    public function getLogin(): string
101
    {
102
        return $this->login;
103
    }
104
105
    public function setLogin(string $login)
106
    {
107
        $this->login = $login;
108
    }
109
110
    public function getPassword(): string
111
    {
112
        return $this->password;
113
    }
114
115
    public function setPassword(string $password)
116
    {
117
        $this->password = $password;
118
    }
119
}
120