Completed
Push — master ( 65e840...33fe4c )
by Gabriel
03:16
created

Request::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
        } catch (\Exception $error) {
41
            throw new Exception($error->getMessage(), $error->getCode(), $error);
42
        }
43
        
44
        return new Response($reponse);
45
    }
46
47
    private function getBody(array $data): string
48
    {
49
        $content = file_get_contents($this->template);
50
51
        $data = array_merge($data, [
52
          'ThreePLID' => $this->getId(),
53
          'Login' => $this->getLogin(),
54
          'Password' => $this->getPassword(),
55
          'CustomerID' => $this->getCustomerId(),
56
          'FacilityID' => $this->getFacilityId()
57
        ]);
58
59
        foreach ($data as $prop => $value) {
60
            $content = str_replace("{{$prop}}", $value, $content);
61
        }
62
63
        return $content;
64
    }
65
66
    public function setTemplate(string $template)
67
    {
68
        $this->template = $template;
69
    }
70
71
    public function getId(): string
72
    {
73
        return $this->id;
74
    }
75
76
    public function setId(string $id)
77
    {
78
        $this->id = $id;
79
    }
80
81
    public function getCustomerId(): string
82
    {
83
        return $this->customerId;
84
    }
85
86
    public function setCustomerId(string $customerId)
87
    {
88
        $this->customerId = $customerId;
89
    }
90
91
    public function getFacilityId(): string
92
    {
93
        return $this->facilityId;
94
    }
95
96
    public function setFacilityId(string $facilityId)
97
    {
98
        $this->facilityId = $facilityId;
99
    }
100
101
    public function getLogin(): string
102
    {
103
        return $this->login;
104
    }
105
106
    public function setLogin(string $login)
107
    {
108
        $this->login = $login;
109
    }
110
111
    public function getPassword(): string
112
    {
113
        return $this->password;
114
    }
115
116
    public function setPassword(string $password)
117
    {
118
        $this->password = $password;
119
    }
120
121
    public function getUrl(): string
122
    {
123
        return $this->url;
124
    }
125
}
126