Skip to content

Commit 5f132b6

Browse files
authored
Merge pull request #26 from clue-labs/internal
Mark all classes as final and all internal APIs as @internal
2 parents f103214 + fc74a39 commit 5f132b6

File tree

7 files changed

+127
-15
lines changed

7 files changed

+127
-15
lines changed

src/Client.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33
namespace Clue\React\Soap;
44

55
use Clue\React\Buzz\Browser;
6-
use Exception;
7-
use Clue\React\Soap\Protocol\ClientEncoder;
86
use Clue\React\Soap\Protocol\ClientDecoder;
7+
use Clue\React\Soap\Protocol\ClientEncoder;
98
use Psr\Http\Message\ResponseInterface;
109
use React\Promise\Deferred;
1110

12-
class Client
11+
final class Client
1312
{
1413
private $wsdl;
1514
private $browser;
1615
private $encoder;
1716
private $decoder;
1817

18+
/**
19+
* [internal] Instantiate new SOAP client, see Factory instead
20+
*
21+
* @param string $wsdl
22+
* @param Browser $browser
23+
* @param ClientEncoder $encoder
24+
* @param ClientDecoder $decoder
25+
* @internal
26+
*/
1927
public function __construct($wsdl, Browser $browser, ClientEncoder $encoder = null, ClientDecoder $decoder = null)
2028
{
2129
if ($encoder === null) {
@@ -43,21 +51,20 @@ public function soapCall($name, $args)
4351
}
4452

4553
return $this->browser->send($request)->then(
46-
array($this, 'handleResponse'),
47-
array($this, 'handleError')
54+
array($this, 'handleResponse')
4855
);
4956
}
5057

58+
/**
59+
* @param ResponseInterface $response
60+
* @return mixed
61+
* @internal
62+
*/
5163
public function handleResponse(ResponseInterface $response)
5264
{
5365
return $this->decoder->decode((string)$response->getBody());
5466
}
5567

56-
public function handleError(Exception $error)
57-
{
58-
throw $error;
59-
}
60-
6168
public function getFunctions()
6269
{
6370
return $this->encoder->__getFunctions();
@@ -87,7 +94,7 @@ public function getTypes()
8794
*
8895
* @param string|int $function
8996
* @return string
90-
* @throws SoapFault if given function does not exist
97+
* @throws \SoapFault if given function does not exist
9198
* @see self::getFunctions()
9299
*/
93100
public function getLocation($function)

src/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Clue\React\Buzz\Browser;
77
use Psr\Http\Message\ResponseInterface;
88

9-
class Factory
9+
final class Factory
1010
{
1111
private $loop;
1212
private $browser;

src/Protocol/ClientDecoder.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use \SoapClient;
66

7-
class ClientDecoder extends SoapClient
7+
/**
8+
* @internal
9+
*/
10+
final class ClientDecoder extends SoapClient
811
{
912
private $response = null;
1013

@@ -15,6 +18,13 @@ public function __construct()
1518
parent::__construct(null, array('location' => '1', 'uri' => '2'));
1619
}
1720

21+
/**
22+
* Decodes the SOAP response / return value from the given SOAP envelope (HTTP response body)
23+
*
24+
* @param string $response
25+
* @return mixed
26+
* @throws \SoapFault if response indicates a fault (error condition) or is invalid
27+
*/
1828
public function decode($response)
1929
{
2030
// temporarily save response internally for further processing
@@ -28,6 +38,16 @@ public function decode($response)
2838
return $ret;
2939
}
3040

41+
/**
42+
* Overwrites the internal request logic to parse the response
43+
*
44+
* By overwriting this method, we can skip the actual request sending logic
45+
* and still use the internal parsing logic by injecting the response as
46+
* the return code in this method. This will implicitly be invoked by the
47+
* call to `pseudoCall()` in the above `decode()` method.
48+
*
49+
* @see SoapClient::__doRequest()
50+
*/
3151
public function __doRequest($request, $location, $action, $version, $one_way = 0)
3252
{
3353
// the actual result doesn't actually matter, just return the given result

src/Protocol/ClientEncoder.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@
55
use \SoapClient;
66
use RingCentral\Psr7\Request;
77

8-
class ClientEncoder extends SoapClient
8+
/**
9+
* @internal
10+
*/
11+
final class ClientEncoder extends SoapClient
912
{
1013
private $request = null;
1114

15+
/**
16+
* Encodes the given RPC function name and arguments as a SOAP request
17+
*
18+
* @param string $name
19+
* @param array $args
20+
* @return Request
21+
* @throws \SoapFault if request is invalid according to WSDL
22+
*/
1223
public function encode($name, $args)
1324
{
1425
$this->__soapCall($name, $args);
@@ -19,6 +30,18 @@ public function encode($name, $args)
1930
return $request;
2031
}
2132

33+
/**
34+
* Overwrites the internal request logic to build the request message
35+
*
36+
* By overwriting this method, we can skip the actual request sending logic
37+
* and still use the internal request serializing logic by accessing the
38+
* given `$request` parameter and building our custom request object from
39+
* it. We skip/ignore its parsing logic by returing an empty response here.
40+
* This will implicitly be invoked by the call to `__soapCall()` in the
41+
* above `encode()` method.
42+
*
43+
* @see SoapClient::__doRequest()
44+
*/
2245
public function __doRequest($request, $location, $action, $version, $one_way = 0)
2346
{
2447
$this->request = new Request(

src/Proxy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Clue\React\Soap;
44

5-
class Proxy
5+
final class Proxy
66
{
7+
private $client;
8+
79
public function __construct(Client $client)
810
{
911
$this->client = $client;

tests/Protocol/ClientDecoderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Clue\React\Soap\Protocol\ClientDecoder;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class ClientDecoderTest extends TestCase
7+
{
8+
/**
9+
* @expectedException SoapFault
10+
*/
11+
public function testDecodeThrowsSoapFaultForInvalidResponse()
12+
{
13+
$decoder = new ClientDecoder();
14+
$decoder->decode('invalid');
15+
}
16+
}

tests/Protocol/ClientEncoderTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use Clue\React\Soap\Protocol\ClientEncoder;
5+
use Psr\Http\Message\RequestInterface;
6+
7+
class ClientEncoderTest extends TestCase
8+
{
9+
public function testEncodeCreatesRequestForNonWsdlRpcFunction()
10+
{
11+
$encoder = new ClientEncoder(null, array('location' => 'http://example.com/soap', 'uri' => 'demo'));
12+
13+
$request = $encoder->encode('add', array('first' => 10, 'second' => 20));
14+
15+
$this->assertTrue($request instanceof RequestInterface);
16+
$this->assertSame('POST', $request->getMethod());
17+
$this->assertSame('http://example.com/soap', (string)$request->getUri());
18+
$this->assertSame('text/xml; charset=utf-8', $request->getHeaderLine('Content-Type'));
19+
}
20+
21+
/**
22+
* @expectedException SoapFault
23+
*/
24+
public function testConstructorThrowsWhenUrlIsInvalid()
25+
{
26+
if (extension_loaded('xdebug')) {
27+
$this->markTestSkipped('Invalid WSDL causes a fatal error when ext-xdebug is loaded');
28+
}
29+
30+
new ClientEncoder('invalid');
31+
}
32+
33+
/**
34+
* @expectedException SoapFault
35+
*/
36+
public function testConstructorThrowsWhenNonWsdlDoesNotDefineLocationAndUri()
37+
{
38+
if (extension_loaded('xdebug')) {
39+
$this->markTestSkipped('Invalid non-WSDL mode causes a fatal error when ext-xdebug is loaded');
40+
}
41+
42+
new ClientEncoder(null);
43+
}
44+
}

0 commit comments

Comments
 (0)