Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Commit

Permalink
Fix Laravel 5.4 compatibility by removing arguments in container bind…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
percymamedy committed Feb 19, 2017
1 parent 5b33777 commit c791a75
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 49 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.1.x-dev"
"dev-master": "1.2.x-dev"
}
},
"minimum-stability": "dev",
Expand Down
50 changes: 16 additions & 34 deletions src/AbstractPersonalityInsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace FindBrok\PersonalityInsights;

use FindBrok\PersonalityInsights\Models\Profile;
use FindBrok\PersonalityInsights\Auth\AccessManager;
use FindBrok\PersonalityInsights\Support\DataCollector\ContentItem;
use FindBrok\PersonalityInsights\Support\DataCollector\ContentListContainer;
Expand Down Expand Up @@ -57,7 +56,7 @@ abstract class AbstractPersonalityInsights implements InsightsContract
public function newUpContainer($contentItems = [])
{
// New Up Container.
$this->contentListContainer = app(ContentListContainer::SERVICE_ID, $contentItems);
$this->contentListContainer = app(ContentListContainer::SERVICE_ID)->setContentsItems($contentItems);
}

/**
Expand All @@ -81,7 +80,7 @@ public function getContainer()
public function usingCredentials($name = null)
{
// Set credentials name.
$this->credentialsName = config('personality-insights.credentials.'.$name);
$this->getAccessManager()->setCredentialsName($name)->setCredentials($name);

// Return this.
return $this;
Expand All @@ -97,7 +96,7 @@ public function usingCredentials($name = null)
public function usingApiVersion($apiVersion = null)
{
// Set credentials name.
$this->apiVersion = $apiVersion;
$this->getAccessManager()->setApiVersion($apiVersion);

// Return this.
return $this;
Expand All @@ -110,7 +109,7 @@ public function usingApiVersion($apiVersion = null)
*/
public function getCredentialsName()
{
return $this->credentialsName ?: config('personality-insights.default_credentials');
return $this->getAccessManager()->getCredentialsName();
}

/**
Expand All @@ -120,7 +119,7 @@ public function getCredentialsName()
*/
public function getApiVersion()
{
return $this->apiVersion ?: config('personality-insights.api_version');
return $this->getAccessManager()->getApiVersion();
}

/**
Expand All @@ -132,7 +131,8 @@ protected function getHeaders()
{
// Return headers.
return collect($this->headers)
->merge(['X-Watson-Learning-Opt-Out' => config('personality-insights.x_watson_learning_opt_out')])->all();
->merge(['X-Watson-Learning-Opt-Out' => config('personality-insights.x_watson_learning_opt_out')])
->all();
}

/**
Expand Down Expand Up @@ -167,14 +167,14 @@ protected function getQuery()
protected function sendRequest()
{
// Get AccessManager.
$accessManager = $this->makeAccessManager();
$accessManager = $this->getAccessManager();

// Make a Watson Bridge.
$watsonBridge = $this->makeBridge($accessManager);
$watsonBridge = $this->makeBridge();

// Cross the Bridge and return the Response.
return $watsonBridge->post($accessManager->getProfileResourcePath().$this->getQuery(), $this->getContainer()
->getContentsForRequest());
return $watsonBridge->post($accessManager->getProfileResourcePath().$this->getQuery(),
$this->getContainer()->getContentsForRequest());
}

/**
Expand Down Expand Up @@ -230,32 +230,24 @@ public function withQuery(array $query = [])
}

/**
* Creates and returns a new instance of AccessManager.
* Returns the AccessManager.
*
* @return AccessManager
*/
public function makeAccessManager()
public function getAccessManager()
{
return app(AccessManager::SERVICE_ID, [
'credentialsName' => $this->getCredentialsName(),
'apiVersion' => $this->getApiVersion(),
]);
return app(AccessManager::SERVICE_ID);
}

/**
* Create a new WatsonBridge to handle Requests.
*
* @param AccessManager|null $accessManager
*
* @return \FindBrok\WatsonBridge\Bridge;
*/
public function makeBridge(AccessManager $accessManager = null)
public function makeBridge()
{
// Make AccessManager if its not Present.
$accessManager = $accessManager ?: $this->makeAccessManager();

// Create and Return Bridge.
return app('PIBridge', $accessManager->getCredentials())->appendHeaders($this->getHeaders());
return app('PIBridge')->appendHeaders($this->getHeaders());
}

/**
Expand Down Expand Up @@ -312,14 +304,4 @@ public function cacheLifetime()
{
return config('personality-insights.cache_expiration');
}

/**
* Checks if profile data is already loaded in profile prop.
*
* @return bool
*/
public function hasProfilePreLoaded()
{
return property_exists($this, 'profile') && ! is_null($this->profile) && $this->profile instanceof Profile;
}
}
52 changes: 52 additions & 0 deletions src/Auth/AccessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class AccessManager
*/
protected $credentials = [];

/**
* Credentials name to use.
*
* @var string
*/
protected $credentialsName;

/**
* The API Version we are using.
*
Expand All @@ -33,10 +40,45 @@ class AccessManager
*/
public function __construct($credentialsName = null, $apiVersion = null)
{
$this->setCredentialsName($credentialsName);
$this->setCredentials($credentialsName);
$this->setApiVersion($apiVersion);
}

/**
* Sets the Credentials name.
*
* @param string $credentialsName
*
* @return $this
*/
public function setCredentialsName($credentialsName = null)
{
// No credentials name.
if (is_null($credentialsName)) {
$credentialsName = config('personality-insights.default_credentials');
}

// Credentials does not exist.
if (! config()->has('personality-insights.credentials.'.$credentialsName)) {
throw new InvalidCredentialsName;
}

$this->credentialsName = $credentialsName;

return $this;
}

/**
* Gets the Credentials name to use.
*
* @return null|string
*/
public function getCredentialsName()
{
return $this->credentialsName;
}

/**
* Returns the Credentials to use for requests.
*
Expand Down Expand Up @@ -112,4 +154,14 @@ public function getProfileResourcePath()
{
return $this->getApiVersion().'/profile';
}

/**
* Gets the Default Credentials.
*
* @return array
*/
public function getDefaultCredentials()
{
return config('personality-insights.credentials.'.config('personality-insights.default_credentials'));
}
}
56 changes: 44 additions & 12 deletions src/InsightsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use FindBrok\WatsonBridge\Bridge;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
use FindBrok\PersonalityInsights\Auth\AccessManager;
use FindBrok\PersonalityInsights\Facades\PersonalityInsightsFacade;
use FindBrok\PersonalityInsights\Support\DataCollector\ContentListContainer;
Expand Down Expand Up @@ -46,28 +48,58 @@ public function register()
*
* @return void
*/
public function registerBindings()
protected function registerBindings()
{
// Bind Personality Insights interface.
$this->app->bind(InsightsContract::class, PersonalityInsights::class);

// Bind Personality Insights Service.
$this->app->bind(PersonalityInsights::SERVICE_ID, PersonalityInsights::class);

// Registers the Access Manager.
$this->registerAccessManager();

// Registers the Bridge.
$this->registerBridge();

// Bind PersonalityInsights ContentListContainer in App.
$this->app->bind(ContentListContainer::SERVICE_ID, function () {
return (new ContentListContainer)->cleanContainer();
});
}

/**
* Registers the Access Manager in
* the Container.
*
* @return void
*/
protected function registerAccessManager()
{
// Bind AccessManager.
$this->app->bind(AccessManager::SERVICE_ID,
function ($app, $args = ['credentialsName' => 'default', 'apiVersion' => 'v3']) {
return new AccessManager($args['credentialsName'], $args['apiVersion']);
});
$this->app->singleton(AccessManager::SERVICE_ID, function (Application $app) {
/** @var Repository $configRepo */
$configRepo = $app->make('config');

// Bind WatsonBridge for Personality insights that we depend on.
$this->app->bind('PIBridge', function ($app, $args = ['username' => '', 'password' => '', 'url' => '']) {
return new Bridge($args['username'], $args['password'], $args['url']);
return new AccessManager($configRepo->get('personality-insights.default_credentials'),
$configRepo->get('personality-insights.api_version'));
});
}

// Bind PersonalityInsights ContentListContainer in App.
$this->app->bind(ContentListContainer::SERVICE_ID, function ($app, $contentItems = []) {
return (new ContentListContainer($contentItems))->cleanContainer();
/**
* Registers the Bridge.
*
* @return void
*/
protected function registerBridge()
{
// Bind WatsonBridge for Personality insights that we depend on.
$this->app->bind('PIBridge', function (Application $app) {
// Get Default Credentials.
$credentials = $app->make(AccessManager::SERVICE_ID)->getCredentials();

// Return an Instance of Bridge.
return new Bridge($credentials['username'], $credentials['password'], $credentials['url']);
});
}

Expand All @@ -76,7 +108,7 @@ function ($app, $args = ['credentialsName' => 'default', 'apiVersion' => 'v3'])
*
* @return void
*/
public function registerFacades()
protected function registerFacades()
{
// Since Laravel 5.4 allows for automatic Facade
// we do not need to register Facades here.
Expand Down
10 changes: 10 additions & 0 deletions src/PersonalityInsights.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ public function clean()
// Return calling object.
return $this;
}

/**
* Checks if profile data is already loaded in profile prop.
*
* @return bool
*/
public function hasProfilePreLoaded()
{
return property_exists($this, 'profile') && ! is_null($this->profile) && $this->profile instanceof Profile;
}
}
18 changes: 16 additions & 2 deletions src/Support/DataCollector/ContentListContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ public function __construct($items = [])
parent::__construct($items);
}

/**
* Sets Contents to the Container.
*
* @param array $items
*
* @return $this
*/
public function setContentsItems($items = [])
{
$this->items = $this->getArrayableItems($items);

return $this;
}

/**
* Remove all invalid contents in the ContentListContainer.
*
Expand All @@ -44,8 +58,8 @@ public function cleanContainer()
public function getCacheKey()
{
// Return Key.
return 'PersonalityInsights-'.Uuid::uuid5(Uuid::NAMESPACE_DNS, collect(['contentItems' => $this->toArray()])->toJson())
->toString();
return 'PersonalityInsights-'.Uuid::uuid5(Uuid::NAMESPACE_DNS,
collect(['contentItems' => $this->toArray()])->toJson())->toString();
}

/**
Expand Down

0 comments on commit c791a75

Please sign in to comment.