src/Backend/Facebook.php
<?php
namespace Heise\Shariff\Backend;
use Psr\Http\Message\RequestInterface;
/**
* Class Facebook.
*/
class Facebook extends Request implements ServiceInterface
{
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'facebook';
}
/**
* {@inheritdoc}
*/
public function setConfig(array $config): void
{
if (empty($config['app_id']) || empty($config['secret'])) {
throw new \InvalidArgumentException('The Facebook app_id and secret must not be empty.');
}
parent::setConfig($config);
}
/**
* {@inheritdoc}
*/
public function getRequest(string $url): RequestInterface
{
$accessToken = urlencode($this->config['app_id']) . '|' . urlencode($this->config['secret']);
$query = 'https://graph.facebook.com/v18.0/?id='
. urlencode($url) . '&fields=og_object%7Bengagement%7D&access_token=' . $accessToken;
return new \GuzzleHttp\Psr7\Request('GET', $query);
}
/**
* {@inheritdoc}
*/
public function extractCount(array $data): int
{
return (int)($data['og_object']['engagement']['count'] ?? 0);
}
}