SilverstripeFacebookConnector::get_feed()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 7
rs 10
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
     *
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 = "";
0 ignored issues
show
introduced by
The private property $app_id is not used, and could be removed.
Loading history...
34
35
    /**
36
     * application secret - get from FB
37
     *
38
     * @var string
39
     */
40
    private static $app_secret = "";
0 ignored issues
show
introduced by
The private property $app_secret is not used, and could be removed.
Loading history...
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
Bug introduced by
The type SunnysideUp\ShareThis\Facebook\Facebook was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
        }
82
        return self::$connection;
83
    }
84
85
    /**
86
     *
87
     * @param string $openGraphCommand
88
     *
89
     * @return FacebookResponse | false
0 ignored issues
show
Bug introduced by
The type SunnysideUp\ShareThis\FacebookResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 type SunnysideUp\ShareThis\Fa...cebookResponseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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 type SunnysideUp\ShareThis\Fa...ns\FacebookSDKException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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 exist on SilverStripe\Dev\Debug. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            Debug::/** @scrutinizer ignore-call */ 
109
                   log(implode(" | ", self::$error));

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
0 ignored issues
show
Bug introduced by
The type SunnysideUp\ShareThis\details was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
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