Completed
Push — master ( e75dc2...732184 )
by Nicolaas
42:54 queued 29:46
created

SilverstripeFacebookConnector::run_command()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
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
     * @var array
24
     */
25
    private static $connection_config = array();
26
27
    /**
28
     * application ID - get from FB
29
     * @var string
30
     */
31
    private static $app_id = "";
32
33
    /**
34
     * application secret - get from FB
35
     * @var string
36
     */
37
    private static $app_secret = "";
38
39
40
    /**
41
     * debug
42
     * @var boolean
43
     */
44
    protected static $debug = false;
45
46
    /**
47
     * keep track of errors
48
     * @var array
49
     */
50
    protected static $error = array();
51
52
    /**
53
     * set additional connection details - e.g. default_access_token
54
     * @param array
55
     */
56
    public static function set_connection_config($connectionConfig)
57
    {
58
        self::$connection_config = $connectionConfig;
59
    }
60
61
    /**
62
     * create FB connection...
63
     * @return Facebook\Facebook
64
     */
65
    protected static function get_connection()
66
    {
67
        if (!self::$connection) {
68
            self::$connection_config += array(
69
                'app_id' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_id"),
70
                'app_secret' => Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret"),
71
                'default_graph_version' => 'v2.4',
72
                //'default_access_token' => '{access-token}', // optional
73
            );
74
75
            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...
76
        }
77
        return self::$connection;
78
    }
79
80
    /**
81
     *
82
     * @param string $openGraphCommand
83
     *
84
     * @return FacebookResponse | false
85
     */
86
    public static function run_command($openGraphCommand = "")
87
    {
88
        $fb = self::get_connection();
89
        $accessToken = Config::inst()->get(SilverstripeFacebookConnector::class, "app_id")."|".Config::inst()->get(SilverstripeFacebookConnector::class, "app_secret");
90
        //$helper = $fb->getPageTabHelper();
91
        try {
92
            $response = $fb->get($openGraphCommand, $accessToken);
93
        } 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...
94
            // When Graph returns an error
95
            self::$error[] = 'Graph returned an error: ' . $e->getMessage();
96
            return false;
97
        } 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...
98
            // When validation fails or other local issues
99
            self::$error[] = 'Facebook SDK returned an error: ' . $e->getMessage();
100
            return false;
101
        }
102
        if (self::$debug) {
103
            Debug::log(implode(" | ", self::$error));
104
        }
105
        return $response;
106
    }
107
108
    /**
109
     * @return details about logged in person
110
     */
111
    public static function whoami()
112
    {
113
        $response = self::run_command("/me");
114
        if ($response) {
115
            return $response->getGraphUser();
116
        }
117
    }
118
119
120
    /**
121
     * returns an array of recent posts for a page
122
     * @return array
123
     */
124
    public static function get_feed($pageID)
125
    {
126
        $response = self::run_command($pageID . "/posts?fields=message,created_time,id,full_picture,link,from,name,description");
127
        if ($response) {
128
            $list = $response->getDecodedBody();
129
            if (isset($list["data"])) {
130
                return $list["data"];
131
            }
132
        }
133
    }
134
135
    /**
136
     * returns an array of recent posts for a page
137
     * @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...
138
     */
139
    public static function check_if_posts_exists($UID)
140
    {
141
        $response = self::run_command('/Post/'.$UID);
142
        print_r($response);
143
    }
144
}
145