Skip to content

Commit

Permalink
Create a singleton to avoid connection each time you call artisan
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLombry committed Oct 7, 2016
1 parent ef4c5a8 commit 8996fb0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

<?php
namespace Davispeixoto\LaravelSalesforce;

use Illuminate\Support\ServiceProvider;
use Davispeixoto\ForceDotComToolkitForPhp\SforceEnterpriseClient as Client;

/**
* Class LaravelSalesforceServiceProvider
* @package Davispeixoto\LaravelSalesforce
*/
class LaravelSalesforceServiceProvider extends ServiceProvider
{
/**
Expand All @@ -30,8 +34,11 @@ public function boot()
*/
public function register()
{
$this->app['salesforce'] = $this->app->share(function ($app) {
return new Salesforce($app['config']);
$this->app['salesforce'] = $this->app->singleton('salesforce', function ($app) {
$sf = new Salesforce(new Client());
$sf->connect($app['config']);

return $sf;
});
}

Expand Down
56 changes: 32 additions & 24 deletions src/Davispeixoto/LaravelSalesforce/Salesforce.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Davispeixoto\ForceDotComToolkitForPhp\SforceEnterpriseClient as Client;
use Exception;
use Illuminate\Config\Repository;

/**
* Class Salesforce
Expand All @@ -15,50 +14,59 @@
*/
class Salesforce
{

/**
* @var Client $sfh The Salesforce Handler
*/
private $sfh;
public $sfh;

/**
* The constructor.
* Salesforce constructor.
*
* @param Client $sfh
* @throws SalesforceException
*/
public function __construct(Client $sfh)
{
$this->sfh = $sfh;
}

/**
* @param $method
* @param $args
* @return mixed
*/
public function __call($method, $args)
{
return call_user_func_array(array($this->sfh, $method), $args);
}

/**
* Authenticates into Salesforce according to
* the provided credentials and WSDL file
*
* @param Repository $configExternal
* @param $configExternal
* @throws SalesforceException
*/
public function __construct(Repository $configExternal)
public function connect($configExternal)
{
try {
$this->sfh = new Client();
$wsdl = $configExternal->get('salesforce.wsdl');

$wsdl = $configExternal->get('laravel-salesforce::wsdl');
if (empty($wsdl)) {
$wsdl = __DIR__ . '/Wsdl/enterprise.wsdl.xml';
}

if (empty($wsdl)) {
$wsdl = __DIR__ . '/Wsdl/enterprise.wsdl.xml';
}
$user = $configExternal->get('salesforce.username');
$pass = $configExternal->get('salesforce.password');
$token = $configExternal->get('salesforce.token');

try {
$this->sfh->createConnection($wsdl);

$username = $configExternal->get('laravel-salesforce::username');
$password = $configExternal->get('laravel-salesforce::password');
$token = $configExternal->get('laravel-salesforce::token');

$this->sfh->login($username, $password . $token);

$this->sfh->login($user, $pass . $token);
} catch (Exception $e) {
throw new SalesforceException('Exception at Constructor' . $e->getMessage() . "\n\n" . $e->getTraceAsString());
}
}

public function __call($method, $args)
{
return call_user_func_array(array($this->sfh, $method), $args);
}

/*
* Debugging functions
*/
Expand Down

0 comments on commit 8996fb0

Please sign in to comment.