SalesforceController.php in Salesforce Suite 8.3
File
src/Controller/SalesforceController.php
View source
<?php
namespace Drupal\salesforce\Controller;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\MetadataBubblingUrlGenerator;
use Drupal\salesforce\Rest\RestClientInterface;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class SalesforceController extends ControllerBase {
protected $client;
protected $httpClient;
protected $url_generator;
public function __construct(RestClientInterface $rest, Client $httpClient, MetadataBubblingUrlGenerator $url_generator) {
$this->client = $rest;
$this->httpClient = $httpClient;
$this->url_generator = $url_generator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('salesforce.client'), $container
->get('http_client'), $container
->get('url_generator'));
}
protected function request() {
return \Drupal::request();
}
protected function successMessage() {
drupal_set_message(t('Successfully connected to %endpoint', [
'%endpoint' => $this->client
->getInstanceUrl(),
]));
}
public function oauthCallback() {
if (empty($this
->request()
->get('code'))) {
throw new AccessDeniedHttpException();
}
$data = urldecode(UrlHelper::buildQuery([
'code' => $this
->request()
->get('code'),
'grant_type' => 'authorization_code',
'client_id' => $this->client
->getConsumerKey(),
'client_secret' => $this->client
->getConsumerSecret(),
'redirect_uri' => $this->client
->getAuthCallbackUrl(),
]));
$url = $this->client
->getAuthTokenUrl();
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$response = $this->httpClient
->post($url, [
'headers' => $headers,
'body' => $data,
]);
$this->client
->handleAuthResponse($response);
$this
->successMessage();
return new RedirectResponse($this->url_generator
->generateFromRoute('salesforce.authorize', [], [
"absolute" => TRUE,
], FALSE));
}
}