Skip to content

Commit

Permalink
Merge pull request #8 from dachcom-digital/use_page_tokens_1.x
Browse files Browse the repository at this point in the history
Use page tokens 1.x
  • Loading branch information
pascalmoser authored Nov 23, 2022
2 parents 1f00706 + 0e69bd7 commit 286493c
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 91 deletions.
6 changes: 5 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Upgrade Notes
![upgrade](https://user-images.githubusercontent.com/700119/31535145-3c01a264-affa-11e7-8d86-f04c33571f65.png)

## 1.1.0
- [BUGFIX] Use Page Access Token [#6](https://github.com/dachcom-digital/pimcore-social-data-facebook-connector/issues/6)

***

![upgrade](https://user-images.githubusercontent.com/700119/31535145-3c01a264-affa-11e7-8d86-f04c33571f65.png)

After every update you should check the pimcore extension manager.
Just click the "update" button or execute the migration command to finish the bundle update.

Expand Down
15 changes: 12 additions & 3 deletions src/Builder/SocialPostBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function configureFetch(BuildConfig $buildConfig, OptionsResolver $resolv
->fields([$posts]);

$resolver->setDefaults([
'facebookQueryBuilder' => $queryBuilder
'facebookQueryBuilder' => $queryBuilder,
'pageId' => $feedConfiguration->getPageId(),
]);

$resolver->setRequired(['facebookQueryBuilder']);
Expand All @@ -105,11 +106,19 @@ public function fetch(FetchData $data): void

/** @var FacebookQueryBuilder $fqbRequest */
$fqbRequest = $options['facebookQueryBuilder'];
$pageId = $options['pageId'];

$url = $fqbRequest->asEndpoint();

$accessToken = $engineConfiguration->getAccessToken();

if ($pageId !== null) {
$pageAccessToken = $engineConfiguration->getPageConfig($pageId, 'accessToken');
$accessToken = $pageAccessToken ?? $accessToken;
}

try {
$response = $client->get($url, $engineConfiguration->getAccessToken());
$response = $client->get($url, $accessToken);
} catch (FacebookResponseException $e) {
throw new BuildException(sprintf('graph error: %s [endpoint: %s]', $e->getMessage(), $url));
} catch (FacebookSDKException $e) {
Expand Down Expand Up @@ -200,7 +209,7 @@ public function transform(TransformData $data): void
$socialPost->setSocialCreationDate($creationTime);
$socialPost->setContent($element['message']);
$socialPost->setUrl($element['permalink_url']);
$socialPost->setPosterUrl($element['full_picture']);
$socialPost->setPosterUrl($element['full_picture'] ?? null);

$data->setTransformedElement($socialPost);
}
Expand Down
61 changes: 60 additions & 1 deletion src/Controller/Admin/FacebookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace SocialData\Connector\Facebook\Controller\Admin;

use Carbon\Carbon;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use Pimcore\Bundle\AdminBundle\Controller\AdminController;
use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
use SocialData\Connector\Facebook\Client\FacebookClient;
use SocialData\Connector\Facebook\Model\EngineConfiguration;
use SocialDataBundle\Connector\ConnectorDefinitionInterface;
use SocialDataBundle\Controller\Admin\Traits\ConnectResponseTrait;
use SocialDataBundle\Exception\BuildException;
use SocialDataBundle\Service\ConnectorServiceInterface;
use SocialDataBundle\Service\EnvironmentServiceInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -132,25 +134,82 @@ public function checkAction(Request $request)

$connectorEngineConfig->setAccessToken($accessToken->getValue(), true);
$connectorEngineConfig->setAccessTokenExpiresAt($expiresAt, true);

// now set page tokens
$client = $this->facebookClient->getClient($connectorEngineConfig = $this->getConnectorEngineConfig($this->getConnectorDefinition()));
$accessToken = $connectorEngineConfig->getAccessToken();

$url = '/me/accounts?fields=name,access_token';

try {
$response = $client->get($url, $accessToken);
} catch (FacebookResponseException $e) {
throw new BuildException(sprintf('graph error: %s [endpoint: %s]', $e->getMessage(), $url));
} catch (FacebookSDKException $e) {
throw new BuildException(sprintf('facebook SDK error: %s [endpoint: %s]', $e->getMessage(), $url));
}

$pageTokens = $response->getDecodedBody();

$pages = [];
foreach ($pageTokens['data'] ?? [] as $page) {
$pages[] = [
'id' => $page['id'] ?? null,
'name' => $page['name'] ?? null,
'accessToken' => $page['access_token'] ?? null,
];
}

$connectorEngineConfig->setPages($pages);

$this->connectorService->updateConnectorEngineConfiguration('facebook', $connectorEngineConfig);

return $this->buildConnectSuccessResponse();
}

public function feedConfigAction(Request $request): JsonResponse
{
$feedConfig = [];

try {
$connectorEngineConfig = $this->getConnectorEngineConfig($this->getConnectorDefinition());
} catch (\Throwable $e) {
return $this->adminJson(['error' => true, 'message' => $e->getMessage()]);
}

if ($connectorEngineConfig->hasPages()) {
$feedConfig['pages'] = array_values(array_map(static function (array $page) {
return ['key' => $page['name'] ?? $page['id'], 'value' => $page['id']];
}, $connectorEngineConfig->getPages()));
}

return $this->adminJson([
'success' => true,
'data' => $feedConfig
]);
}


/**
* @param Request $request
*
* @return JsonResponse
*/
public function debugTokenAction(Request $request)
{
$pageId = $request->query->get('pageId', null);

try {
$connectorEngineConfig = $this->getConnectorEngineConfig($this->getConnectorDefinition());
} catch (\Throwable $e) {
return $this->adminJson(['error' => true, 'message' => $e->getMessage()]);
}

$token = $connectorEngineConfig->getAccessToken();
if ($pageId !== null && $connectorEngineConfig->hasPages()) {
$token = $connectorEngineConfig->getPageConfig($pageId, 'accessToken');
} else {
$token = $connectorEngineConfig->getAccessToken();
}

if (empty($token)) {
return $this->adminJson(['error' => true, 'message' => 'acccess token is empty']);
Expand Down
54 changes: 54 additions & 0 deletions src/Model/EngineConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class EngineConfiguration implements ConnectorEngineConfigurationInterface
*/
protected $appSecret;

/**
* @var array
*/
protected $pages = [];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -116,4 +121,53 @@ public function getAppSecret()
{
return $this->appSecret;
}

/**
* @param array|null $pages
*/
public function setPages(?array $pages)
{
if (!is_array($pages)) {
return;
}

$this->pages = $pages;
}

/**
* @return array
*/
public function getPages()
{
return $this->pages;
}

/**
* @return bool
*/
public function hasPages()
{
return count($this->pages) > 0;
}

/**
* @param $pageId
* @param string $config
*
* @return false|mixed|null
*/
public function getPageConfig($pageId, string $config)
{
if ($this->hasPages() === false) {
return false;
}

foreach ($this->getPages() as $page) {
if ($page['id'] === $pageId) {
return $page[$config] ?? null;
}
}

return null;
}
}
6 changes: 5 additions & 1 deletion src/Resources/config/pimcore/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ social_data_connector_facebook_connect_check:

social_data_connector_facebook_debug_token:
path: /admin/social-data/connector/facebook/debug-token
defaults: { _controller: SocialData\Connector\Facebook\Controller\Admin\FacebookController::debugTokenAction }
defaults: { _controller: SocialData\Connector\Facebook\Controller\Admin\FacebookController::debugTokenAction }

social_data_connector_facebook_feed_config:
path: /admin/social-data/connector/facebook/feed-config
defaults: { _controller: SocialData\Connector\Facebook\Controller\Admin\FacebookController::feedConfigAction }
Loading

0 comments on commit 286493c

Please sign in to comment.