Skip to content

Commit 2a8b72f

Browse files
committed
Fixes #1: Added a more extensible connectivity layer to the Blizzard API that uses cURL to analyse requests - thus removing the file_get_contents_dependency - and integrated support for Authorization tokens.
1 parent a942319 commit 2a8b72f

File tree

7 files changed

+232
-22
lines changed

7 files changed

+232
-22
lines changed

Service/ApiService.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class ApiService
2828
*/
2929
protected $callService = null;
3030

31+
/**
32+
* @var string
33+
*/
34+
protected $authorizationToken = null;
35+
3136
/**
3237
* @param CallService $callService The service that makes the actual API calls. As long as the API stays read-only, it will strictly be used as an extra dependency in order to mock the call to the WS.
3338
*/
@@ -219,4 +224,23 @@ protected function getNormalisedPlayerProfileArray($apiData = array())
219224
$apiData['career'] = array_merge($normalisedArray['career'], $apiData['career']);
220225
return $apiData;
221226
}
227+
228+
/**
229+
* @param string $authorizationToken
230+
* @return $this
231+
*/
232+
public function setAuthorizationToken($authorizationToken)
233+
{
234+
$this->authorizationToken = $authorizationToken;
235+
$this->callService->getConnectivityLayer()->getBlizzardApi()->setAuthorizationToken($authorizationToken);
236+
return $this;
237+
}
238+
239+
/**
240+
* @return string
241+
*/
242+
public function getAuthorizationToken()
243+
{
244+
return $this->authorizationToken;
245+
}
222246
}

Service/CallService.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22

33
namespace petrepatrasc\BlizzardApiBundle\Service;
44
use petrepatrasc\BlizzardApiBundle\Entity\Exception\BlizzardApiException;
5+
use petrepatrasc\StarcraftConnectionLayerBundle\Service\ConnectionService;
56

67
/**
78
* Handles calling the Battle.NET service. Added for extensibility at the moment.
89
* @package petrepatrasc\BlizzardApiBundle\Service
910
*/
1011
class CallService
1112
{
13+
/**
14+
* @var ConnectionService
15+
*/
16+
protected $connectivityLayer;
17+
18+
/**
19+
* Class constructor.
20+
*/
21+
public function __construct() {
22+
$this->connectivityLayer = new ConnectionService();
23+
}
1224

1325
/**
1426
* Make a call to the Battle.NET Api Service.
@@ -29,12 +41,30 @@ public function makeCallToApiService($region, $apiMethod, $params = array(), $tr
2941
$battleNetUrl .= '/';
3042
}
3143

32-
$result = @file_get_contents($battleNetUrl);
44+
$result = $this->getConnectivityLayer()->getBlizzardApi()->retrieveData($battleNetUrl);
3345

34-
if ($result === FALSE) {
35-
throw new BlizzardApiException("Failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found", 404);
46+
if ($result === null) {
47+
throw new BlizzardApiException("No data was returned from the server", 500);
3648
} else {
3749
return $result;
3850
}
3951
}
52+
53+
/**
54+
* @param \petrepatrasc\StarcraftConnectionLayerBundle\Service\ConnectionService $connectivityLayer
55+
* @return $this
56+
*/
57+
public function setConnectivityLayer($connectivityLayer)
58+
{
59+
$this->connectivityLayer = $connectivityLayer;
60+
return $this;
61+
}
62+
63+
/**
64+
* @return \petrepatrasc\StarcraftConnectionLayerBundle\Service\ConnectionService
65+
*/
66+
public function getConnectivityLayer()
67+
{
68+
return $this->connectivityLayer;
69+
}
4070
}

Tests/Integration/ApiServiceTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public function testGetProfileSanityCheckThatApiIsActuallyResponding()
2929
$this->assertEquals("/profile/2048419/1/LionHeart/", $profile->getBasicInformation()->getProfilePath());
3030
}
3131

32+
/**
33+
* @expectedException \petrepatrasc\BlizzardApiBundle\Entity\Exception\BlizzardApiException
34+
* @expectedExceptionMessage Invalid Application
35+
*/
36+
public function testThatAnInvalidApplicationKeyWillNotWork() {
37+
$this->apiService->setAuthorizationToken("BNET c1fbf21b79c03191d:+3fE0RaKc+PqxN0gi8va5GQC35A=");
38+
$this->apiService->getPlayerProfile(\petrepatrasc\BlizzardApiBundle\Entity\Region::Europe, 2048419, 'LionHeart');
39+
}
40+
3241
public function testGetPlayerLatestMatches()
3342
{
3443
$matches = $this->apiService->getPlayerLatestMatches(Entity\Region::Europe, 2048419, 'LionHeart');
@@ -40,6 +49,7 @@ public function testGetPlayerLatestMatches()
4049
* @var $match Entity\Match
4150
* @var $latestMatch Entity\Match
4251
*/
52+
$this->assertInternalType('array', $matches);
4353
foreach ($matches as $match) {
4454
$this->assertInstanceOf('\petrepatrasc\BlizzardApiBundle\Entity\Match', $match);
4555
$this->assertInternalType('string', $match->getMap());

Tests/Unit/Service/ApiServiceTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setUp()
2323
{
2424
parent::setUp();
2525

26-
$this->callServiceMock = $this->getMock('CallService', array('makeCallToApiService'));
26+
$this->callServiceMock = $this->getMock('CallService', array('makeCallToApiService', 'getConnectivityLayer'));
2727

2828
$this->apiService = new ApiService($this->callServiceMock);
2929
}
@@ -298,6 +298,7 @@ public function testGetPlayerLatestMatches()
298298
* @var $match Entity\Match
299299
* @var $latestMatch Entity\Match
300300
*/
301+
$this->assertInternalType('array', $matches);
301302
foreach ($matches as $match) {
302303
$this->assertInstanceOf('\petrepatrasc\BlizzardApiBundle\Entity\Match', $match);
303304
$this->assertInternalType('string', $match->getMap());
@@ -653,4 +654,18 @@ public function testMakeCallWhenExceptionIsHit() {
653654
$this->callServiceMock->expects($this->atLeastOnce())->method('makeCallToApiService')->withAnyParameters()->will($this->returnValue(file_get_contents(self::MOCK_PATH . 'resource-not-found.json')));
654655
$callResponse = $this->apiService->makeCall(Entity\Region::Europe, ApiService::API_REWARDS_METHOD, array(), false);
655656
}
657+
658+
public function testThatSettingAnAuthorizationKeyIsPropagatedToAllLayers() {
659+
$blizzardServiceMock = $this->getMock('\petrepatrasc\StarcraftConnectionLayer\Service\BlizzardApi', array('setAuthorizationToken'));
660+
$blizzardServiceMock->expects($this->atLeastOnce())->method('setAuthorizationToken')->withAnyParameters()->will($this->returnValue($blizzardServiceMock));
661+
662+
$connectivityLayerMock = $this->getMock('\petrepatrasc\StarcraftConnectionLayer\Service\ConnectionService', array('getBlizzardApi'));
663+
$connectivityLayerMock->expects($this->atLeastOnce())->method('getBlizzardApi')->withAnyParameters()->will($this->returnValue($blizzardServiceMock));
664+
665+
$this->callServiceMock->expects($this->atLeastOnce())->method('getConnectivityLayer')->withAnyParameters()->will($this->returnValue($connectivityLayerMock));
666+
$sampleAuthorizationToken = "TEST";
667+
668+
$this->apiService->setAuthorizationToken($sampleAuthorizationToken);
669+
$this->assertEquals($sampleAuthorizationToken, $this->apiService->getAuthorizationToken());
670+
}
656671
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace petrepatrasc\BlizzardApiBundle\Tests\Unit\Service;
4+
5+
6+
use petrepatrasc\BlizzardApiBundle\Entity;
7+
use petrepatrasc\BlizzardApiBundle\Service\ApiService;
8+
use petrepatrasc\BlizzardApiBundle\Service\CallService;
9+
use petrepatrasc\StarcraftConnectionLayerBundle\Service\ConnectionService;
10+
11+
class CallServiceTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var CallService
15+
*/
16+
protected $callService;
17+
18+
public function setUp() {
19+
parent::setUp();
20+
21+
$this->callService = new CallService();
22+
}
23+
24+
public function testDependencyInjection() {
25+
$connectivityLayer = new ConnectionService();
26+
27+
$this->callService = new CallService();
28+
$this->callService->setConnectivityLayer($connectivityLayer);
29+
30+
$this->assertEquals($connectivityLayer, $this->callService->getConnectivityLayer());
31+
}
32+
33+
/**
34+
* @expectedException \petrepatrasc\BlizzardApiBundle\Entity\Exception\BlizzardApiException
35+
* @expectedExceptionCode 500
36+
* @expectedExceptionMessage No data was returned from the server
37+
*/
38+
public function testNullResponseRaisesExceptions() {
39+
$blizzardServiceMock = $this->getMock('\petrepatrasc\StarcraftConnectionLayer\Service\BlizzardApi', array('retrieveData'));
40+
$blizzardServiceMock->expects($this->atLeastOnce())->method('retrieveData')->withAnyParameters()->will($this->returnValue(null));
41+
42+
$connectivityLayerMock = $this->getMock('\petrepatrasc\StarcraftConnectionLayer\Service\ConnectionService', array('getBlizzardApi'));
43+
$connectivityLayerMock->expects($this->atLeastOnce())->method('getBlizzardApi')->withAnyParameters()->will($this->returnValue($blizzardServiceMock));
44+
45+
$this->callService->setConnectivityLayer($connectivityLayerMock);
46+
47+
$this->callService->makeCallToApiService(Entity\Region::Europe, ApiService::API_REWARDS_METHOD, array(), false);
48+
}
49+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"php": ">=5.3.2",
1717
"ext-curl": "*",
1818
"symfony/framework-bundle": ">=2.1",
19-
"symfony/security-bundle": ">=2.1"
19+
"symfony/security-bundle": ">=2.1",
20+
"petrepatrasc/starcraft-connection-layer": ">=1.0.0"
2021
},
2122
"require-dev": {
2223
"twig/twig": "*",

0 commit comments

Comments
 (0)