1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Borfast\Socializr\Connectors; |
4
|
|
|
|
5
|
|
|
use Borfast\Socializr\Exceptions\GenericPostingException; |
6
|
|
|
use Borfast\Socializr\Post; |
7
|
|
|
use Borfast\Socializr\Group; |
8
|
|
|
use Borfast\Socializr\Response; |
9
|
|
|
|
10
|
|
|
// use \Requests; |
11
|
|
|
|
12
|
|
|
class FacebookGroup extends Facebook |
13
|
|
|
{ |
14
|
|
|
/** @var Group */ |
15
|
|
|
protected $group = null; |
16
|
|
|
|
17
|
|
|
public function post(Post $post) |
18
|
|
|
{ |
19
|
|
|
$group = $this->getGroup(); |
20
|
|
|
|
21
|
|
|
if (empty($post->media)) { |
22
|
|
|
$path = '/'.$group->id.'/feed'; |
23
|
|
|
|
24
|
|
|
$msg = $post->title; |
25
|
|
|
$msg .= "\n\n"; |
26
|
|
|
$msg .= $post->body; |
27
|
|
|
$msg = trim($msg); |
28
|
|
|
|
29
|
|
|
$params = [ |
30
|
|
|
// 'caption' => $post->title, |
|
|
|
|
31
|
|
|
'description' => '', |
32
|
|
|
'link' => $post->url, |
33
|
|
|
'message' => $msg |
34
|
|
|
]; |
35
|
|
View Code Duplication |
} else { |
36
|
|
|
$path = '/'.$group->id.'/photos'; |
37
|
|
|
|
38
|
|
|
$msg = $post->title; |
39
|
|
|
$msg .= "\n\n"; |
40
|
|
|
$msg .= $post->body; |
41
|
|
|
$msg .= "\n"; |
42
|
|
|
$msg .= $post->url; |
43
|
|
|
|
44
|
|
|
$params = [ |
45
|
|
|
'url' => $post->media[0], |
46
|
|
|
'caption' => $msg |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$method = 'POST'; |
51
|
|
|
|
52
|
|
|
$result = $this->request($path, $method, $params); |
53
|
|
|
$json_result = json_decode($result, true); |
54
|
|
|
|
55
|
|
|
// If there's no ID, the post didn't go through |
56
|
|
|
if (!isset($json_result['id'])) { |
57
|
|
|
$msg = "Unknown error posting to Facebook group."; |
58
|
|
|
throw new GenericPostingException($msg, 1); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$response = new Response; |
62
|
|
|
$response->setRawResponse($result); // This is already JSON. |
63
|
|
|
$response->setProvider('Facebook'); |
64
|
|
|
$response->setPostId($json_result['id']); |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getGroup() |
70
|
|
|
{ |
71
|
|
|
if (is_null($this->group)) { |
72
|
|
|
$path = '/' . $this->id . '?fields=id,name,icon'; |
73
|
|
|
$result = $this->request($path); |
74
|
|
|
$json_result = json_decode($result, true); |
75
|
|
|
|
76
|
|
|
$mapping = [ |
77
|
|
|
'id' => 'id', |
78
|
|
|
'name' => 'name', |
79
|
|
|
'picture' => 'icon' |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$this->group = Group::create($mapping, $json_result); |
83
|
|
|
$this->group->picture = $json_result['icon']; |
84
|
|
|
$this->group->link = 'https://www.facebook.com/groups/' . $json_result['id']; |
85
|
|
|
$this->group->can_post = true; |
86
|
|
|
$this->group->provider = static::$provider; |
87
|
|
|
$this->group->raw_response = $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->group; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the number of memebers this group has. |
95
|
|
|
*/ |
96
|
|
|
public function getStats() |
97
|
|
|
{ |
98
|
|
|
return $this->getMembersCount(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/*************************************************************************** |
103
|
|
|
* |
104
|
|
|
* From here on these are FacebookGroup-specific methods that should not be |
105
|
|
|
* accessed from other classes. |
106
|
|
|
* |
107
|
|
|
**************************************************************************/ |
108
|
|
|
|
109
|
|
View Code Duplication |
protected function getMembersCount() |
110
|
|
|
{ |
111
|
|
|
$group = $this->getGroup(); |
112
|
|
|
|
113
|
|
|
$path = '/'.$group->id.'/members'; |
114
|
|
|
$result = $this->request($path); |
115
|
|
|
|
116
|
|
|
$response = json_decode($result); |
117
|
|
|
$response = count($response->data); |
118
|
|
|
|
119
|
|
|
return $response; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.