|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Christian Gripp <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Core23\ShariffBundle\Service; |
|
11
|
|
|
|
|
12
|
|
|
use Core23\ShariffBundle\Service\Exception\FetchException; |
|
13
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
14
|
|
|
use Psr\Http\Message\RequestInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class Facebook implements Service |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $appid; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $secret; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $version; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(string $appid, string $secret, ?string $version = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->appid = $appid; |
|
37
|
|
|
$this->secret = $secret; |
|
38
|
|
|
$this->version = $version; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getCode(): string |
|
42
|
|
|
{ |
|
43
|
|
|
return 'facebook'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function createRequest(RequestFactoryInterface $factory, string $url): RequestInterface |
|
47
|
|
|
{ |
|
48
|
|
|
$accessToken = urlencode($this->appid).'|'.urlencode($this->secret); |
|
49
|
|
|
|
|
50
|
|
|
$query = sprintf( |
|
51
|
|
|
'https://graph.facebook.com/%s?id=%s&fields=engagement&access_token=%s', |
|
52
|
|
|
$this->getVersionPrefix(), |
|
53
|
|
|
urlencode($url), |
|
54
|
|
|
$accessToken |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return $factory->createRequest('GET', $query); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function count(ResponseInterface $response): int |
|
61
|
|
|
{ |
|
62
|
|
|
$json = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); |
|
63
|
|
|
|
|
64
|
|
|
if (!\array_key_exists('engagement', $json)) { |
|
65
|
|
|
throw new FetchException('The "engagement" attributes could not be found', $response->getBody()->getContents()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (isset( |
|
69
|
|
|
$json['engagement']['reaction_count'], |
|
70
|
|
|
$json['engagement']['comment_count'], |
|
71
|
|
|
$json['engagement']['share_count'] |
|
72
|
|
|
)) { |
|
73
|
|
|
return $json['engagement']['reaction_count'] |
|
74
|
|
|
+ $json['engagement']['comment_count'] |
|
75
|
|
|
+ $json['engagement']['share_count']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return 0; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function getVersionPrefix(): string |
|
82
|
|
|
{ |
|
83
|
|
|
if (null === $this->version) { |
|
84
|
|
|
return ''; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return 'v'.ltrim($this->version, 'v').'/'; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|