From 9f5cf79d0645225e7c35c3ccb5043a7cb7728994 Mon Sep 17 00:00:00 2001 From: richard67 Date: Sat, 4 Nov 2017 11:52:07 +0100 Subject: [PATCH 1/6] Use facebook api v2.10 Use current facebook api version v2.10. Beginning with v2.9, the api requires the fields parameter to be set if it shall return not only the id but additional fields, and the fields have been reorganised and renamed. --- src/Backend/Facebook.php | 9 +++------ tests/FacebookTest.php | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Backend/Facebook.php b/src/Backend/Facebook.php index cc63179..159bb5e 100644 --- a/src/Backend/Facebook.php +++ b/src/Backend/Facebook.php @@ -32,7 +32,7 @@ public function setConfig(array $config) public function getRequest($url) { $accessToken = urlencode($this->config['app_id']) .'|'.urlencode($this->config['secret']); - $query = 'https://graph.facebook.com/v2.8/?id='.urlencode($url).'&access_token='.$accessToken; + $query = 'https://graph.facebook.com/v2.10/?id='.urlencode($url).'&fields=engagement&access_token='.$accessToken; return new \GuzzleHttp\Psr7\Request('GET', $query); } @@ -42,11 +42,8 @@ public function getRequest($url) */ public function extractCount(array $data) { - if (isset($data['data']) && isset($data['data'][0]) && isset($data['data'][0]['total_count'])) { - return $data['data'][0]['total_count']; - } - if (isset($data['share']) && isset($data['share']['share_count'])) { - return $data['share']['share_count']; + if (isset($data['engagement']) && isset($data['engagement']['reaction_count']) && isset($data['engagement']['comment_count']) && isset($data['engagement']['share_count'])) { + return $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + $data['engagement']['share_count']; } return 0; diff --git a/tests/FacebookTest.php b/tests/FacebookTest.php index 99716a3..46d972b 100644 --- a/tests/FacebookTest.php +++ b/tests/FacebookTest.php @@ -31,9 +31,9 @@ public function testUsesGraphApi() $request = $facebook->getRequest('http://www.heise.de'); $this->assertEquals('graph.facebook.com', $request->getUri()->getHost()); - $this->assertEquals('/v2.8/', $request->getUri()->getPath()); + $this->assertEquals('/v2.10/', $request->getUri()->getPath()); $this->assertEquals( - 'id='.urlencode('http://www.heise.de').'&access_token=foo%7Cbar', + 'id='.urlencode('http://www.heise.de').'&fields=engagement&access_token=foo%7Cbar', $request->getUri()->getQuery() ); } From 8610fcb4a10930007f8f3ee3294d6f2819167af0 Mon Sep 17 00:00:00 2001 From: richard67 Date: Sat, 4 Nov 2017 13:00:56 +0100 Subject: [PATCH 2/6] Code style correction 1 Fix too long lines --- src/Backend/Facebook.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Backend/Facebook.php b/src/Backend/Facebook.php index 159bb5e..a263642 100644 --- a/src/Backend/Facebook.php +++ b/src/Backend/Facebook.php @@ -32,7 +32,8 @@ public function setConfig(array $config) public function getRequest($url) { $accessToken = urlencode($this->config['app_id']) .'|'.urlencode($this->config['secret']); - $query = 'https://graph.facebook.com/v2.10/?id='.urlencode($url).'&fields=engagement&access_token='.$accessToken; + $query = 'https://graph.facebook.com/v2.10/?id='.urlencode($url) . '&fields=engagement&access_token=' + . $accessToken; return new \GuzzleHttp\Psr7\Request('GET', $query); } @@ -42,8 +43,10 @@ public function getRequest($url) */ public function extractCount(array $data) { - if (isset($data['engagement']) && isset($data['engagement']['reaction_count']) && isset($data['engagement']['comment_count']) && isset($data['engagement']['share_count'])) { - return $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + $data['engagement']['share_count']; + if (isset($data['engagement']) && isset($data['engagement']['reaction_count']) + && isset($data['engagement']['comment_count']) && isset($data['engagement']['share_count'])) { + return $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + + $data['engagement']['share_count']; } return 0; From 68d10a3970dea0c1bfa941cafcb2415b01a726e7 Mon Sep 17 00:00:00 2001 From: richard67 Date: Sat, 4 Nov 2017 20:11:51 +0100 Subject: [PATCH 3/6] Simplify isset check and add some new lines Simplify isset check and add some new lines for better readability --- src/Backend/Facebook.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Backend/Facebook.php b/src/Backend/Facebook.php index a263642..659a96a 100644 --- a/src/Backend/Facebook.php +++ b/src/Backend/Facebook.php @@ -43,9 +43,11 @@ public function getRequest($url) */ public function extractCount(array $data) { - if (isset($data['engagement']) && isset($data['engagement']['reaction_count']) - && isset($data['engagement']['comment_count']) && isset($data['engagement']['share_count'])) { - return $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + if (isset($data['engagement']['reaction_count']) + && isset($data['engagement']['comment_count']) + && isset($data['engagement']['share_count'])) { + return $data['engagement']['reaction_count'] + + $data['engagement']['comment_count'] + $data['engagement']['share_count']; } From 7ecdfe896fcd1847cbc5ee2133b4ace4ff44ec46 Mon Sep 17 00:00:00 2001 From: richard67 Date: Sun, 5 Nov 2017 12:54:22 +0100 Subject: [PATCH 4/6] Use 1 call to isset only for all 3 elements --- src/Backend/Facebook.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Backend/Facebook.php b/src/Backend/Facebook.php index 659a96a..f8f25d1 100644 --- a/src/Backend/Facebook.php +++ b/src/Backend/Facebook.php @@ -43,9 +43,9 @@ public function getRequest($url) */ public function extractCount(array $data) { - if (isset($data['engagement']['reaction_count']) - && isset($data['engagement']['comment_count']) - && isset($data['engagement']['share_count'])) { + if (isset($data['engagement']['reaction_count'], + $data['engagement']['comment_count'], + $data['engagement']['share_count'])) { return $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + $data['engagement']['share_count']; From 9915d9efe02d17497b6ee1296f3575c44abf367d Mon Sep 17 00:00:00 2001 From: richard67 Date: Sun, 5 Nov 2017 13:02:05 +0100 Subject: [PATCH 5/6] Code style correction 2 Correct multi-line function call --- src/Backend/Facebook.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Backend/Facebook.php b/src/Backend/Facebook.php index f8f25d1..129b3c9 100644 --- a/src/Backend/Facebook.php +++ b/src/Backend/Facebook.php @@ -43,9 +43,11 @@ public function getRequest($url) */ public function extractCount(array $data) { - if (isset($data['engagement']['reaction_count'], - $data['engagement']['comment_count'], - $data['engagement']['share_count'])) { + if (isset( + $data['engagement']['reaction_count'], + $data['engagement']['comment_count'], + $data['engagement']['share_count'] + )) { return $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + $data['engagement']['share_count']; From fb5386d28c07b0d2fa4f128b0bd738abd594df59 Mon Sep 17 00:00:00 2001 From: Alexander Pluhar Date: Mon, 20 Nov 2017 12:10:53 +0100 Subject: [PATCH 6/6] fix code style --- src/Backend/Facebook.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Backend/Facebook.php b/src/Backend/Facebook.php index 129b3c9..f8d05f8 100644 --- a/src/Backend/Facebook.php +++ b/src/Backend/Facebook.php @@ -47,7 +47,7 @@ public function extractCount(array $data) $data['engagement']['reaction_count'], $data['engagement']['comment_count'], $data['engagement']['share_count'] - )) { + )) { return $data['engagement']['reaction_count'] + $data['engagement']['comment_count'] + $data['engagement']['share_count'];