Completed
Pull Request — master (#9)
by
unknown
08:34
created

SilverstripeFacebookConnector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A set_connection_config() 0 4 1
A get_connection() 0 14 2
A run_command() 0 21 4
A whoami() 0 7 2
A get_feed() 0 10 3
A check_if_posts_exists() 0 5 1
1
<?php
2
3
namespace SunnysideUp\ShareThis;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\Debug;
8
9
/**
10
 * https://developers.facebook.com/tools-and-support/
11
 */
12
class SilverstripeFacebookConnector
13
{
14
    use Injectable;
15
16
    /**
17
     * @var Facebook Connection
18
     */
19
    private static $connection = null;
20
21
    /**
22
     * settings for connection
23
     *
24
     * @var array
25
     */
26
    private static $connection_config = [];
27
28
    /**
29
     * application ID - get from FB
30
     *
31
     * @var string
32
     */
33
    private static $app_id = "";
34
35
    /**
36
     * application secret - get from FB
37
     *
38
     * @var string
39
     */
40
    private static $app_secret = "";
41
42
43
    /**
44
     * debug
45
     *
46
     * @var boolean
47
     */
48
    protected static $debug = false;
49
50
    /**
51
     * keep track of errors
52
     * @var array
53
     */
54
    protected static $error = [];
55
56
    /**
57
     * set additional connection details - e.g. default_access_token
58
     *
59
     * @param array
60
     */
61
    public static function set_connection_config($connectionConfig)
62
    {
63
        self::$connection_config = $connectionConfig;
64
    }
65
66
    /**
67
     * create FB connection...
68
     * @return Facebook\Facebook
69
     */
70
    protected static function get_connection()
71
    {
72
        if (!self::$connection) {
73
            self::$connection_config += [
74
                'app_id' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_id"),
75
                'app_secret' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret"),
76
                'default_graph_version' => 'v2.4',
77
                //'default_access_token' => '{access-token}', // optional
78
            ];
79
80
            self::$connection = new Facebook\Facebook(self::$connection_config);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SunnysideUp\ShareTh...lf::$connection_config) of type object<SunnysideUp\ShareThis\Facebook\Facebook> is incompatible with the declared type object<SunnysideUp\ShareThis\Facebook> of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        }
82
        return self::$connection;
83
    }
84
85
    /**
86
     *
87
     * @param string $openGraphCommand
88
     *
89
     * @return FacebookResponse | false
90
     */
91
    public static function run_command($openGraphCommand = "")
92
    {
93
        $fb = self::get_connection();
94
        $accessToken = Config::inst()->get(SilverstripeFacebookConnector::class, "app_id")."|".Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret");
95
        //$helper = $fb->getPageTabHelper();
96
        try {
97
            $response = $fb->get($openGraphCommand, $accessToken);
98
        } catch (Facebook\Exceptions\FacebookResponseException $e) {
0 ignored issues
show
Bug introduced by
The class SunnysideUp\ShareThis\Fa...cebookResponseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
99
            // When Graph returns an error
100
            self::$error[] = 'Graph returned an error: ' . $e->getMessage();
101
            return false;
102
        } catch (Facebook\Exceptions\FacebookSDKException $e) {
0 ignored issues
show
Bug introduced by
The class SunnysideUp\ShareThis\Fa...ns\FacebookSDKException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
103
            // When validation fails or other local issues
104
            self::$error[] = 'Facebook SDK returned an error: ' . $e->getMessage();
105
            return false;
106
        }
107
        if (self::$debug) {
108
            Debug::log(implode(" | ", self::$error));
0 ignored issues
show
Bug introduced by
The method log() does not seem to exist on object<SilverStripe\Dev\Debug>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
        }
110
        return $response;
111
    }
112
113
    /**
114
     * @return details about logged in person
115
     */
116
    public static function whoami()
117
    {
118
        $response = self::run_command("/me");
119
        if ($response) {
120
            return $response->getGraphUser();
121
        }
122
    }
123
124
125
    /**
126
     * returns an array of recent posts for a page
127
     *
128
     * @return array
129
     */
130
    public static function get_feed($pageID)
131
    {
132
        $response = self::run_command($pageID . "/posts?fields=message,created_time,id,full_picture,link,from,name,description");
133
        if ($response) {
134
            $list = $response->getDecodedBody();
135
            if (isset($list["data"])) {
136
                return $list["data"];
137
            }
138
        }
139
    }
140
141
    /**
142
     * returns an array of recent posts for a page
143
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
144
     */
145
    public static function check_if_posts_exists($UID)
146
    {
147
        $response = self::run_command('/Post/'.$UID);
148
        print_r($response);
149
    }
150
}
151