Skip to content

Commit

Permalink
move configs to an entity; remove all uses of app
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Anderson committed Sep 10, 2015
1 parent 1309e5f commit 3ac6c02
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 103 deletions.
36 changes: 19 additions & 17 deletions src/Credibility/LaravelCybersource/Cybersource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
use Credibility\LaravelCybersource\Exceptions\CybersourceException;
use Credibility\LaravelCybersource\models\CybersourceResponse;
use Credibility\LaravelCybersource\models\CybersourceSOAPModel;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\App;
use Credibility\LaravelCybersource\Configs\Factory as ConfigsFactory;

class Cybersource {

/**
* @var Illuminate\Foundation\Application
*/
public $app;
/**
* @var SOAPRequester
*/
private $requester;
public $timeout = 10;

/* ServerConfigs */
protected $configs;

public $avsCodes = array(
'A' => 'Partial match: Street address matches, but 5-digit and 9-digit postal codes do not match.',
'B' => 'Partial match: Street address matches, but postal code is not verified.',
Expand Down Expand Up @@ -79,10 +77,10 @@ class Cybersource {
);


public function __construct($requester, Application $app)
public function __construct($requester)
{
$this->requester = $requester;
$this->app = $app;
$this->configs = (new ConfigsFactory())->getFromConfigFile();
}

// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -269,12 +267,14 @@ public function createCancelSubscriptionRequest($subscriptionId)

public function createNewRequest($merchantReferenceNumber = null)
{
$ref = is_null($merchantReferenceNumber) ? $this->app->make('config')->get('laravel-cybersource::merchant_reference_code') : $merchantReferenceNumber;
if(is_null($merchantReferenceNumber)) {
$merchantReferenceNumber = $this->configs->getMerchantReferenceCode();
}
return new CybersourceSOAPModel(
'PHP', phpversion(),
$this->app->environment(),
$this->app->make('config')->get('laravel-cybersource::merchant_id'),
$ref
$this->configs->getEnv(),
$this->configs->getMerchantId(),
$merchantReferenceNumber
);
}

Expand Down Expand Up @@ -317,10 +317,12 @@ public function getTransactionException($date)
*/
private function sendReportRequest($report_name, $date)
{
$merchant_id = $this->app->make('config')->get('laravel-cybersource::merchant_id');
$endpoint = $this->app->make('config')->get('laravel-cybersource::reports.endpoint');
$username = $this->app->make('config')->get('laravel-cybersource::reports.username');
$password = $this->app->make('config')->get('laravel-cybersource::reports.password');
$merchant_id = $this->configs->getMerchantId();

$reportsArray = $this->configs->getReports();
$endpoint = $reportsArray['endpoint'];
$username = $reportsArray['username'];
$password = $reportsArray['password'];

if ( !$date instanceof \DateTime ) {
$date = new \DateTime($date);
Expand Down Expand Up @@ -384,7 +386,7 @@ private function sendReportRequest($report_name, $date)

private function getTodaysDate()
{
date_default_timezone_set($this->app->make('config')->get('app.timezone'));
date_default_timezone_set($this->configs->getTimezone());
return date('Ymd');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Credibility\LaravelCybersource\Providers;

use Credibility\LaravelCybersource\Configs\Factory as ConfigsFactory;
use Credibility\LaravelCybersource\Cybersource;
use Credibility\LaravelCybersource\SOAPClient;
use Credibility\LaravelCybersource\SOAPClientFactory;
Expand All @@ -8,24 +9,7 @@

class LaravelCybersourceServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$namespace = 'laravel-cybersource';
$path = __DIR__ . '/../../..';
$this->package('credibility/laravel-cybersource', $namespace, $path);
}

/**
* Register the service provider.
Expand All @@ -34,11 +18,12 @@ public function boot()
*/
public function register()
{
$this->app->bind('cybersource', function($app) {
$client = new SOAPClient($app);
$factory = new SOAPClientFactory($app);
$requester = new SOAPRequester($client, $app, $factory);
return new Cybersource($requester, $app);
$this->app->bind('Credibility\LaravelCybersource\Cybersource', function() {
$configs = (new ConfigsFactory())->getFromConfigFile();
$client = new SOAPClient($configs, []);
$factory = new SOAPClientFactory();
$requester = new SOAPRequester($client, $factory);
return new Cybersource($requester);
});
}

Expand Down
11 changes: 5 additions & 6 deletions src/Credibility/LaravelCybersource/SOAPClient.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Credibility\LaravelCybersource;

use BeSimple\SoapClient\SoapClient as BeSimpleSoapClient;
use Credibility\LaravelCybersource\Configs\ServerConfigs;
use Credibility\LaravelCybersource\Exceptions\CybersourceException;

class SOAPClient extends BeSimpleSoapClient {
Expand All @@ -24,13 +25,11 @@ class SOAPClient extends BeSimpleSoapClient {
* Constructs a client off of the
* configured WSDL
*/
public function __construct($app, array $options = [])
public function __construct(ServerConfigs $configs, array $options = [])
{
static::$app = $app;

$this->wsdl = static::$app->make('config')->get('laravel-cybersource::wsdl_endpoint');
$this->merchantId = static::$app->make('config')->get('laravel-cybersource::merchant_id');
$this->transactionId = static::$app->make('config')->get('laravel-cybersource::transaction_id');
$this->wsdl = $configs->getWsdlEndpoint();
$this->merchantId = $configs->getMerchantId();
$this->transactionId = $configs->getTransactionId();

parent::__construct($this->wsdl, $options);
}
Expand Down
12 changes: 3 additions & 9 deletions src/Credibility/LaravelCybersource/SOAPClientFactory.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
<?php namespace Credibility\LaravelCybersource;

use Illuminate\Foundation\Application;
use Credibility\LaravelCybersource\Configs\Factory as ConfigsFactory;

class SOAPClientFactory {

protected $app;

public function __construct($app)
{
$this->app = $app;
}

/**
* Static getInstance Method for updating SOAP options
* @param null $options
* @return SOAPClient
*/
public function getInstance(array $options = [])
{
return new SOAPClient($this->app, $options);
$configs = (new ConfigsFactory())->getFromConfigFile();
return new SOAPClient($configs, $options);
}


Expand Down
12 changes: 5 additions & 7 deletions src/Credibility/LaravelCybersource/SOAPRequester.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php namespace Credibility\LaravelCybersource;

use Credibility\LaravelCybersource\Exceptions\CybersourceConnectionException;
use Credibility\LaravelCybersource\Exceptions\CybersourceException;
use Credibility\LaravelCybersource\Configs\Factory as ConfigsFactory;
use Credibility\LaravelCybersource\models\CybersourceSOAPModel;
use Illuminate\Foundation\Application;

/**
* Class SOAPRequester creates SOAP requests for Cybersource and uses
Expand All @@ -12,20 +11,19 @@
*/
class SOAPRequester {

/** @var Illuminate\Foundation\Application */
public $app;
/** @var SOAPClient */
public $soapClient;
/** @var SOAPClientFactory */
public $clientFactory;
public $timeout;

public function __construct($soapClient, $app, $factory)
public function __construct($soapClient, $factory)
{
$this->app = $app;
$this->soapClient = $soapClient;
$this->clientFactory = $factory;
$this->timeout = $this->app->make('config')->get('laravel-cybersource::timeout');

$configs = (new ConfigsFactory())->getFromConfigFile();
$this->timeout = $configs->getTimeout();
}

public function send(CybersourceSOAPModel $request)
Expand Down
12 changes: 12 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

return array(

/**
* The timezone to be used by cybersource
*/
'env' => 'test',

/**
* The timezone to be used by cybersource
*/
'timezone' => 'America/Los_Angeles',

/**
* The organization ID when creating the cybersource account
*/
Expand Down Expand Up @@ -31,6 +41,8 @@
'endpoint' => 'ebctest.cybersource.com/ebctest',
'version' => '0.1',
'api_version' => '2011-03',
'username' => '',
'password' => '',
),

/**
Expand Down
3 changes: 1 addition & 2 deletions tests/CybersourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public function setUp()
{
parent::setUp();
$this->mockRequester = m::mock('soapRequester');
$this->cybersource = new Cybersource($this->mockRequester, $this->mockApp);
$this->cybersource->app = $this->mockApp;
$this->cybersource = new Cybersource($this->mockRequester);
}

public function testCreateNewRequest()
Expand Down
2 changes: 1 addition & 1 deletion tests/SOAPClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SOAPClientFactoryTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->factory = new SOAPClientFactory($this->mockApp);
$this->factory = new SOAPClientFactory();
}

public function testGetInstanceReturnsSoapClient()
Expand Down
2 changes: 1 addition & 1 deletion tests/SOAPClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SOAPClientTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->client = new SOAPClient($this->mockApp);
$this->client = new SOAPClient($this->configs);
}

public function testConstruct()
Expand Down
2 changes: 1 addition & 1 deletion tests/SOAPRequesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp()
parent::setUp();
$this->mockClient = m::mock('SOAPClient');
$this->factory = m::mock('SOAPFactory');
$this->soapRequester = new SOAPRequester($this->mockClient, $this->mockApp, $this->factory);
$this->soapRequester = new SOAPRequester($this->mockClient, $this->factory);
}

public function testConvertToModelCreatesCybersourceSOAPModel()
Expand Down
40 changes: 3 additions & 37 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,18 @@
namespace LaravelCybersource;

use \Mockery as m;
use Credibility\LaravelCybersource\Configs\Factory as ConfigsFactory;

class TestCase extends \PHPUnit_Framework_TestCase {

protected $environment = 'testing';
protected $merchantId = 'test-merchant-id';
protected $merchantRefCode = 'test-merchant-code';
protected $mockApp;
protected $configs;

public function setUp()
{
$mockConfig = m::mock('Config');

$mockConfig->shouldReceive('get')
->with('laravel-cybersource::merchant_id')
->andReturn($this->merchantId);

$mockConfig->shouldReceive('get')
->with('laravel-cybersource::merchant_reference_code')
->andReturn($this->merchantRefCode);

$mockConfig->shouldReceive('get')
->with('laravel-cybersource::wsdl_endpoint')
->andReturn('https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.26.wsdl');

$mockConfig->shouldReceive('get')
->with('laravel-cybersource::transaction_id')
->andReturn('test_trans_id');

$mockConfig->shouldReceive('get')
->with('laravel-cybersource::timeout')
->andReturn(10);

$mockConfig->shouldReceive('get')
->with('app.timezone')
->andReturn('UTC');

$this->mockApp = m::mock('Illuminate\Foundation\Application');

$this->mockApp
->shouldReceive('environment')
->andReturn($this->environment);

$this->mockApp
->shouldReceive('make')
->with('config')
->andReturn($mockConfig);
$this->configs = (new ConfigsFactory())->getFromConfigFile();
}

public function tearDown()
Expand Down

0 comments on commit 3ac6c02

Please sign in to comment.