social_post_facebook-8.x-1.x-dev/src/Plugin/Network/FacebookPost.php
src/Plugin/Network/FacebookPost.php
<?php
namespace Drupal\social_post_facebook\Plugin\Network;
use Drupal\social_post\Plugin\Network\NetworkBase;
use Drupal\social_api\SocialApiException;
use Drupal\social_post_facebook\Settings\FacebookPostSettings;
use Agaric\FacebookApi\Facebook;
use Drupal\Core\Url;
/**
* Defines Social Post Facebook Network Plugin.
*
* @Network(
* id = "social_post_facebook",
* social_network = "Facebook",
* type = "social_post",
* handlers = {
* "settings": {
* "class": "\Drupal\social_post_facebook\Settings\FacebookPostSettings",
* "config_id": "social_post_facebook.settings"
* }
* }
* )
*/
class FacebookPost extends NetworkBase implements FacebookPostInterface {
/**
* {@inheritdoc}
*/
protected function initSdk(): mixed {
/** @var \Drupal\social_post_facebook\Settings\FacebookPostSettings $settings */
$settings = $this->settings;
if (!$this->validateConfig($settings)) {
throw new SocialApiException('The Social Facebook Configuration is not valid');
}
return new Facebook([
'clientId' => $settings->getAppId(),
'clientSecret' => $settings->getAppSecret(),
'redirectUri' => $settings->getOauthRedirectUrl(),
'graphApiVersion' => 'v' . $settings->getGraphVersion() . '.0',
]);
}
/**
* Checks that module is configured.
*
* @param \Drupal\social_post_facebook\Settings\FacebookPostSettings $settings
* The Facebook auth settings.
*
* @return bool
* True if module is configured.
* False otherwise.
*/
protected function validateConfig(FacebookPostSettings $settings) {
$app_id = $settings->getAppId();
$app_secret = $settings->getAppSecret();
$graph_version = $settings->getGraphVersion();
if (!$app_id || !$app_secret || !$graph_version) {
$this->loggerFactory
->get('social_post_facebook')
->error('Define App ID and App Secret on module settings.');
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getOauthCallback() {
return Url::fromRoute('social_post_facebook.callback')->setAbsolute()->toString();
}
}
