• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / PayWay / 5213927880

pending completion
5213927880

push

github

mlocati
Initial version

1803 of 1803 new or added lines in 47 files covered. (100.0%)

1313 of 1803 relevant lines covered (72.82%)

1.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.55
/src/Client.php
1
<?php
2

3
namespace MLocati\PayWay;
4

5
use RuntimeException;
6

7
class Client
8
{
9
    /**
10
     * @var string always ending with a slash
11
     */
12
    protected $servicesUrlPrefix;
13

14
    /**
15
     * @var string
16
     */
17
    protected $signatureKey;
18

19
    /**
20
     * @var \MLocati\PayWay\Http\Driver
21
     */
22
    protected $driver;
23

24
    /**
25
     * @var callable[]
26
     */
27
    protected $listeners = [];
28

29
    /**
30
     * @var \MLocati\PayWay\Init\Request\Serializer|null
31
     */
32
    private $initSerializer;
33

34
    /**
35
     * @var \MLocati\PayWay\Init\Response\Unserializer|null
36
     */
37
    private $initUnserializer;
38

39
    /**
40
     * @var \MLocati\PayWay\Verify\Request\Serializer|null
41
     */
42
    private $verifySerializer;
43

44
    /**
45
     * @var \MLocati\PayWay\Verify\Response\Unserializer|null
46
     */
47
    private $verifyUnserializer;
48

49
    /**
50
     * @param string $servicesUrl
51
     */
52
    public function __construct($servicesUrl, $signatureKey, Http\Driver $driver = null)
53
    {
54
        $servicesUrlPrefix = filter_var($servicesUrl, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
9✔
55
        if (!$servicesUrlPrefix || strpos($servicesUrlPrefix, '?') !== false) {
9✔
56
            throw new RuntimeException("'{$servicesUrl}' is not a valid base URL of the web services");
5✔
57
        }
58
        $this->servicesUrlPrefix = rtrim($servicesUrl, '/') . '/';
4✔
59
        $this->signatureKey = is_string($signatureKey) ? $signatureKey : '';
4✔
60
        if ($this->signatureKey === '') {
4✔
61
            throw new RuntimeException('Missing the signature key');
1✔
62
        }
63
        $this->driver = $driver === null ? static::buildDefaultDriver() : $driver;
3✔
64
    }
65

66
    /**
67
     * @return \MLocati\PayWay\Init\Response
68
     */
69
    public function init(Init\Request $request)
70
    {
71
        $xml = $this->getInitSerializer()->serialize($request, $this->signatureKey);
3✔
72
        $rawResponse = $this->postSoapRequest('PaymentInitGatewayPort', $xml);
3✔
73
        if (!$rawResponse->is2xx()) {
3✔
74
            throw new Exception\InvalidHttpResponseCode($rawResponse);
1✔
75
        }
76

77
        return $this->getInitUnserializer()->unserialize($rawResponse->getBody());
2✔
78
    }
79

80
    /**
81
     * @return \MLocati\PayWay\Verify\Response
82
     */
83
    public function verify(Verify\Request $request)
84
    {
85
        $xml = $this->getVerifySerializer()->serialize($request, $this->signatureKey);
×
86
        $rawResponse = $this->postSoapRequest('PaymentInitGatewayPort', $xml);
×
87
        if (!$rawResponse->is2xx()) {
×
88
            throw new Exception\InvalidHttpResponseCode($rawResponse);
×
89
        }
90

91
        return $this->getVerifyUnserializer()->unserialize($rawResponse->getBody());
×
92
    }
93

94
    /**
95
     * @return $this
96
     */
97
    public function addListener(callable $value)
98
    {
99
        $this->listeners[] = $value;
1✔
100

101
        return $this;
1✔
102
    }
103

104
    /**
105
     * @return $this
106
     */
107
    public function removeListener(callable $value)
108
    {
109
        $this->listeners = array_filter($this->listeners, static function (callable $item) use ($value) {
1✔
110
            return $item !== $value;
1✔
111
        });
1✔
112

113
        return $this;
1✔
114
    }
115

116
    /**
117
     * @return \MLocati\PayWay\Http\Driver
118
     */
119
    protected static function buildDefaultDriver()
120
    {
121
        if (extension_loaded('curl')) {
×
122
            return new Http\Driver\Curl();
×
123
        }
124

125
        throw new RuntimeException("The cURL extension is required if you don't provide your own HTTP driver");
×
126
    }
127

128
    /**
129
     * @return \MLocati\PayWay\Init\Request\Serializer
130
     */
131
    protected function getInitSerializer()
132
    {
133
        if ($this->initSerializer === null) {
3✔
134
            $this->initSerializer = new Init\Request\Serializer();
3✔
135
        }
136

137
        return $this->initSerializer;
3✔
138
    }
139

140
    /**
141
     * @return \MLocati\PayWay\Init\Response\Unserializer
142
     */
143
    protected function getInitUnserializer()
144
    {
145
        if ($this->initUnserializer === null) {
2✔
146
            $this->initUnserializer = new Init\Response\Unserializer();
2✔
147
        }
148

149
        return $this->initUnserializer;
2✔
150
    }
151

152
    /**
153
     * @return \MLocati\PayWay\Verify\Request\Serializer
154
     */
155
    protected function getVerifySerializer()
156
    {
157
        if ($this->verifySerializer === null) {
×
158
            $this->verifySerializer = new Verify\Request\Serializer();
×
159
        }
160

161
        return $this->verifySerializer;
×
162
    }
163

164
    /**
165
     * @return \MLocati\PayWay\Verify\Response\Unserializer
166
     */
167
    protected function getVerifyUnserializer()
168
    {
169
        if ($this->verifyUnserializer === null) {
×
170
            $this->verifyUnserializer = new Verify\Response\Unserializer();
×
171
        }
172

173
        return $this->verifyUnserializer;
×
174
    }
175

176
    /**
177
     * @param string $path
178
     * @param string $xml
179
     *
180
     * @return \MLocati\PayWay\Http\Response
181
     */
182
    private function postSoapRequest($path, $xml)
183
    {
184
        $url = $this->servicesUrlPrefix . $path;
3✔
185
        $response = $this->driver->send(
3✔
186
            $url,
3✔
187
            'POST',
3✔
188
            [
3✔
189
                'Content-Type' => 'text/xml; charset=utf-8',
3✔
190
                'SOAPAction' => '',
3✔
191
                'Content-Length: ' . strlen($xml),
3✔
192
            ],
3✔
193
            $xml
3✔
194
        );
3✔
195
        if ($this->listeners !== []) {
3✔
196
            $event = new Http\Event($url, 'POST', $xml, $response);
1✔
197
            foreach ($this->listeners as $listener) {
1✔
198
                $listener($event);
1✔
199
            }
200
        }
201

202
        return $response;
3✔
203
    }
204
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc