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

mlocati / PayWay / 5252276091

pending completion
5252276091

push

github

web-flow
Add CLient::normalizeServicesUrl() (#3)

* Add CLient::normalizeServicesUrl()

* Don't run tests if php-cs-fixer fails

18 of 18 new or added lines in 1 file covered. (100.0%)

1590 of 2085 relevant lines covered (76.26%)

1.56 hits per line

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

78.57
/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|object|mixed $servicesUrl
51
     */
52
    public function __construct($servicesUrl, $signatureKey, Http\Driver $driver = null)
53
    {
54
        $servicesUrlPrefix = static::normalizeServicesUrl($servicesUrl);
9✔
55
        if ($servicesUrlPrefix === '') {
9✔
56
            throw new RuntimeException("'{$servicesUrl}' is not a valid base URL of the web services");
5✔
57
        }
58
        $this->servicesUrlPrefix = $servicesUrlPrefix;
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
     * Normalize the value of the services URL.
68
     *
69
     * @param string|object|mixed $value
70
     *
71
     * @return string empty string in case of errors
72
     */
73
    public static function normalizeServicesUrl($value)
74
    {
75
        switch (gettype($value)) {
24✔
76
            case 'string':
24✔
77
                break;
21✔
78
            case 'object':
3✔
79
                if (!method_exists($value, '__toString')) {
1✔
80
                    return '';
×
81
                }
82
                $value = (string) $value;
1✔
83
                break;
1✔
84
            default:
85
                return '';
2✔
86
        }
87
        $value = trim($value);
22✔
88
        if ($value === '' || strpos($value, '?') !== false || strpos($value, '#') !== false) {
22✔
89
            return '';
5✔
90
        }
91
        if (!filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
17✔
92
            return '';
10✔
93
        }
94

95
        return rtrim($value, '/') . '/';
7✔
96
    }
97

98
    /**
99
     * @return \MLocati\PayWay\Init\Response
100
     */
101
    public function init(Init\Request $request)
102
    {
103
        $xml = $this->getInitSerializer()->serialize($request, $this->signatureKey);
3✔
104
        $rawResponse = $this->postSoapRequest('PaymentInitGatewayPort', $xml);
3✔
105
        if (!$rawResponse->is2xx()) {
3✔
106
            throw new Exception\InvalidHttpResponseCode($rawResponse);
1✔
107
        }
108

109
        return $this->getInitUnserializer()->unserialize($rawResponse->getBody());
2✔
110
    }
111

112
    /**
113
     * @return \MLocati\PayWay\Verify\Response
114
     */
115
    public function verify(Verify\Request $request)
116
    {
117
        $xml = $this->getVerifySerializer()->serialize($request, $this->signatureKey);
×
118
        $rawResponse = $this->postSoapRequest('PaymentInitGatewayPort', $xml);
×
119
        if (!$rawResponse->is2xx()) {
×
120
            throw new Exception\InvalidHttpResponseCode($rawResponse);
×
121
        }
122

123
        return $this->getVerifyUnserializer()->unserialize($rawResponse->getBody());
×
124
    }
125

126
    /**
127
     * @return $this
128
     */
129
    public function addListener(callable $value)
130
    {
131
        $this->listeners[] = $value;
1✔
132

133
        return $this;
1✔
134
    }
135

136
    /**
137
     * @return $this
138
     */
139
    public function removeListener(callable $value)
140
    {
141
        $this->listeners = array_filter($this->listeners, static function (callable $item) use ($value) {
1✔
142
            return $item !== $value;
1✔
143
        });
1✔
144

145
        return $this;
1✔
146
    }
147

148
    /**
149
     * @return \MLocati\PayWay\Http\Driver
150
     */
151
    protected static function buildDefaultDriver()
152
    {
153
        if (extension_loaded('curl')) {
×
154
            return new Http\Driver\Curl();
×
155
        }
156

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

160
    /**
161
     * @return \MLocati\PayWay\Init\Request\Serializer
162
     */
163
    protected function getInitSerializer()
164
    {
165
        if ($this->initSerializer === null) {
3✔
166
            $this->initSerializer = new Init\Request\Serializer();
3✔
167
        }
168

169
        return $this->initSerializer;
3✔
170
    }
171

172
    /**
173
     * @return \MLocati\PayWay\Init\Response\Unserializer
174
     */
175
    protected function getInitUnserializer()
176
    {
177
        if ($this->initUnserializer === null) {
2✔
178
            $this->initUnserializer = new Init\Response\Unserializer();
2✔
179
        }
180

181
        return $this->initUnserializer;
2✔
182
    }
183

184
    /**
185
     * @return \MLocati\PayWay\Verify\Request\Serializer
186
     */
187
    protected function getVerifySerializer()
188
    {
189
        if ($this->verifySerializer === null) {
×
190
            $this->verifySerializer = new Verify\Request\Serializer();
×
191
        }
192

193
        return $this->verifySerializer;
×
194
    }
195

196
    /**
197
     * @return \MLocati\PayWay\Verify\Response\Unserializer
198
     */
199
    protected function getVerifyUnserializer()
200
    {
201
        if ($this->verifyUnserializer === null) {
×
202
            $this->verifyUnserializer = new Verify\Response\Unserializer();
×
203
        }
204

205
        return $this->verifyUnserializer;
×
206
    }
207

208
    /**
209
     * @param string $path
210
     * @param string $xml
211
     *
212
     * @return \MLocati\PayWay\Http\Response
213
     */
214
    private function postSoapRequest($path, $xml)
215
    {
216
        $url = $this->servicesUrlPrefix . $path;
3✔
217
        $response = $this->driver->send(
3✔
218
            $url,
3✔
219
            'POST',
3✔
220
            [
3✔
221
                'Content-Type' => 'text/xml; charset=utf-8',
3✔
222
                'SOAPAction' => '',
3✔
223
                'Content-Length: ' . strlen($xml),
3✔
224
            ],
3✔
225
            $xml
3✔
226
        );
3✔
227
        if ($this->listeners !== []) {
3✔
228
            $event = new Http\Event($url, 'POST', $xml, $response);
1✔
229
            foreach ($this->listeners as $listener) {
1✔
230
                $listener($event);
1✔
231
            }
232
        }
233

234
        return $response;
3✔
235
    }
236
}
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