SalesforceAuthProviderPluginManager.php in Salesforce Suite 5.0.x
File
src/SalesforceAuthProviderPluginManager.php
View source
<?php
namespace Drupal\salesforce;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\salesforce\Entity\SalesforceAuthConfig;
use OAuth\Common\Storage\Exception\TokenNotFoundException;
use OAuth\OAuth2\Token\StdOAuth2Token;
class SalesforceAuthProviderPluginManager extends DefaultPluginManager implements SalesforceAuthProviderPluginManagerInterface {
protected $config;
protected $etm;
protected $authStorage;
protected $authConfig;
protected $authProvider;
protected $authCredentials;
protected $authToken;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $etm) {
parent::__construct('Plugin/SalesforceAuthProvider', $namespaces, $module_handler, 'Drupal\\salesforce\\SalesforceAuthProviderInterface');
$this
->alterInfo('salesforce_auth_provider_info');
$this
->setCacheBackend($cache_backend, 'salesforce_auth_provider');
$this->etm = $etm;
}
protected function authStorage() {
if (empty($this->authStorage)) {
$this->authStorage = $this->etm
->getStorage('salesforce_auth');
}
return $this->authStorage;
}
public function getProviders() {
return $this
->authStorage()
->loadMultiple();
}
public function hasProviders() {
return $this
->authStorage()
->hasData();
}
public function getConfig() {
if (!$this->authConfig) {
$provider_id = $this
->config()
->get('salesforce_auth_provider');
if (empty($provider_id)) {
return NULL;
}
$this->authConfig = SalesforceAuthConfig::load($provider_id);
}
return $this->authConfig;
}
public function getProvider() {
if (!$this->authProvider) {
if (!$this
->getConfig()) {
return NULL;
}
$this->authProvider = $this
->getConfig()
->getPlugin();
}
return $this->authProvider;
}
public function getCredentials() {
if (!$this->authCredentials) {
if (!$this
->getProvider()) {
return NULL;
}
$this->authCredentials = $this
->getProvider()
->getCredentials();
}
return $this->authCredentials;
}
public function getToken() {
if (!$this->authToken) {
if (!($config = $this
->getConfig())) {
return NULL;
}
if (!($provider = $config
->getPlugin())) {
return NULL;
}
try {
$this->authToken = $provider
->getAccessToken();
} catch (TokenNotFoundException $e) {
return NULL;
}
}
return $this->authToken;
}
public function refreshToken() {
if (!($config = $this
->getConfig())) {
return NULL;
}
if (!($provider = $config
->getPlugin())) {
return NULL;
}
$token = $this
->getToken() ?: new StdOAuth2Token();
$this->authToken = $provider
->refreshAccessToken($token);
return $this->authToken;
}
protected function config() {
if (!$this->config) {
$this->config = \Drupal::config('salesforce.settings');
}
return $this->config;
}
public function getFallbackPluginId($plugin_id, array $configuration = []) {
return 'broken';
}
}