Permalink
Browse files

create a connector service. Close #4

  • Loading branch information...
1 parent a44127a commit 6777c188848d7efca96aee824f7fb0e579f70cd1 @mgallego committed Aug 13, 2012
View
19 Adapter/WidgetHandler.php
@@ -22,7 +22,12 @@ class WidgetHandler{
private $pushApiPath = 'https://push.ducksboard.com/values/';
private $pullApiPath = 'https://pull.ducksboard.com/values/';
protected $data;
-
+ protected $connector;
+
+
+ public function __construct(Connector $connector){
+ $this->connector = $connector;
+ }
public function push(){
@@ -45,21 +50,27 @@ public function push(){
public function createConnector($apiPath, $method){
- $connector = new Connector($apiPath, $this->apiKey);
+ $connector = $this->connector;
+ $connector->createConnector($apiPath, $this->apiKey);
$connector->setMethod($method);
return $connector;
}
+
public function callApi($apiPath, $method, $inputData = null){
$connector = $this->createConnector($apiPath, $method);
- $connector->setData($inputData);
- $response = $connector->exec();
+ $response = $this->sendData($connector, $inputData);
$connector->close();
return $response;
}
+ public function sendData(&$connector, $data){
+ $connector->setData($data);
+ return $connector->exec();
+ }
+
public function setApiKey($apiKey){
$this->apiKey = $apiKey;
}
View
5 Connection/Connector.php
@@ -15,14 +15,13 @@ class Connector
protected $connector;
- public function __construct($apiPath, $apiKey){
+ public function createConnector($apiPath, $apiKey){
$this->connector = curl_init($apiPath);
curl_setopt($this->connector, CURLOPT_USERPWD, $apiKey.":ignored");
curl_setopt ($this->connector,CURLOPT_RETURNTRANSFER,true);
}
-
-
+
public function setMethod($method = 'POST'){
$methodKey = 1;
View
2 DependencyInjection/SFMDucksboardExtension.php
@@ -13,6 +13,6 @@ public function load(array $configs, ContainerBuilder $container)
{
// registering services
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
- $loader->load('widget.xml');
+ $loader->load('services.xml');
}
}
View
1 README.md
@@ -77,6 +77,7 @@ services:
...
example.ducksboard:
class: SFM\DucksboardBundle\Adapter\Widget
+ arguments: [@sfm.ducksboard.connector]
calls:
- [setApiKey, [%ducksboard_api%]]
View
10 Resources/config/widget.xml → Resources/config/services.xml
@@ -6,11 +6,15 @@
<parameters>
<parameter key="ducksboard_widget.class">SFM\DucksboardBundle\Adapter\Widget</parameter>
+ <parameter key="ducksboard_connector.class">SFM\DucksboardBundle\Connection\Connector</parameter>
</parameters>
<services>
- <service id="sfm.ducksboard.widget" class="%ducksboard_widget.class%">
- </service>
+ <service id="sfm.ducksboard.connector" class="%ducksboard_connector.class%">
+ </service>
+ <service id="sfm.ducksboard.widget" class="%ducksboard_widget.class%">
+ <argument type="service" id="sfm.ducksboard.connector"/>
+ </service>
</services>
-
+
</container>
View
5 Tests/Adapter/WidgetTest.php
@@ -3,20 +3,21 @@
namespace SFM\DucksboardBundle\Tests\Adapter;
use SFM\DucksboardBundle\Adapter\Widget;
+use SFM\DucksboardBundle\Connection\Connector;
class WidgetTest extends \PHPUnit_Framework_TestCase
{
public function testSetData(){
- $widget = new Widget();
+ $widget = new Widget($connector = new Connector());
$data = array(1, array('value'=>1));
$widget->setData($data);
$this->assertEquals($data, $widget->getData());
}
public function testAppendData(){
- $widget = new Widget();
+ $widget = new Widget($connector = new Connector());
$dataOne = array(1, array('value'=>1));
$dataTwo = array(2, array('value'=>2));

0 comments on commit 6777c18

Please sign in to comment.